How to Make a Third Person Character Controller in Unity3D #1 | Camera Controller in 4 Minutes

Поделиться
HTML-код
  • Опубликовано: 17 дек 2024

Комментарии • 10

  • @Rytech_Dev
    @Rytech_Dev  Год назад

    Resources:
    docs.unity3d.com/ScriptReference/Mathf.Atan2.html
    docs.unity3d.com/ScriptReference/Physics.Raycast.html
    docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
    docs.unity3d.com/ScriptReference/Mathf.SmoothDampAngle.html

  • @kalpolproductions8558
    @kalpolproductions8558 Год назад +2

    I just love tutorials when they say "Copy these variables" is all i need

  • @franciscooteiza
    @franciscooteiza Год назад +1

    One word: "Cinemachine"
    the end.

    • @joebwan0240
      @joebwan0240 Год назад +2

      Cinemachine works great. Until it doesn't. If you have a camera design that requires immediate movement or damped movement depending on mouse button state (like many MMOs do) then you are in for a headache. Cinemachine doesn't like it when the dampen values change while it is currently in a dampening state. No fix to this issue that I've found.

  • @Viski982
    @Viski982 Год назад

    Nice work bro ♥️ waiting for the next parts

  • @AyankanM
    @AyankanM 4 месяца назад +1

    The whole script for anyone lazy like me:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class CameraMove : MonoBehaviour
    {
    public LayerMask camRaycastMask;
    public CharacterController controller;
    public Transform cameraTransform;
    public Transform cameraPivot;
    public float controllerLookSmoothTime;
    public float lookSmoothTime;
    public float cameraDistance;
    public float sensitivity;
    public Vector3 pivotOffset;
    public Vector2 xRotationLimits;
    private float yVelocity;
    private float yAngleOffset;
    private float currentControllerYRotation;
    private float targetControllerYRotation;
    private float controllerYRotationVelocity;
    private float currentCameraXRotation;
    private float currentCameraYRotation;
    private float xRotationVelocity;
    private float yRotationVelocity;
    private float targetXRotation;
    private float targetYRotation;
    void Update()
    {
    setCameraLook();
    setControllerLook();
    }
    void setControllerLook()
    {
    float xInput = Input.GetAxisRaw("Horizontal");
    float yInput = Input.GetAxisRaw("Vertical");
    if(xInput * xInput + yInput * yInput > 0)
    {
    targetControllerYRotation = Mathf.Atan2(xInput, yInput) * Mathf.Rad2Deg;
    targetControllerYRotation -= yAngleOffset;
    }
    currentControllerYRotation = Mathf.SmoothDampAngle(currentControllerYRotation, targetControllerYRotation, ref controllerYRotationVelocity, controllerLookSmoothTime);
    controller.transform.eulerAngles = new Vector3(0f, currentControllerYRotation, 0f);
    }
    void setCameraLook()
    {
    if (Input.GetMouseButton(1))
    {
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");
    targetYRotation += mouseX * sensitivity;
    targetXRotation -= mouseY * sensitivity;
    targetXRotation = Mathf.Clamp(targetXRotation, xRotationLimits.x, xRotationLimits.y);
    }
    currentCameraXRotation = Mathf.SmoothDampAngle(currentCameraXRotation, targetXRotation, ref xRotationVelocity, lookSmoothTime);
    currentCameraYRotation = Mathf.SmoothDampAngle(currentCameraYRotation, targetYRotation, ref yRotationVelocity, lookSmoothTime);
    cameraPivot.eulerAngles = new Vector3(currentCameraXRotation, currentCameraYRotation, 0f);
    Ray camRay = new Ray(cameraPivot.position, -cameraPivot.forward);
    float maxDistance = cameraDistance;
    if(Physics.SphereCast(camRay, 0.25f, out RaycastHit hitInfo, cameraDistance, camRaycastMask))
    {
    maxDistance = (hitInfo.point - cameraPivot.position).magnitude - 0.25f;
    }
    cameraTransform.localPosition = Vector3.forward * -(maxDistance - 0.1f);
    yAngleOffset = Mathf.Atan2(cameraPivot.forward.z, cameraPivot.forward.x) * Mathf.Rad2Deg - 90f;
    }
    }

  • @RGBA
    @RGBA Год назад

    Could you do same but with a animated character?

    • @Rytech_Dev
      @Rytech_Dev  Год назад

      Working with animations is a separate task. This series is focusing strictly on the base functionality of third person movement.

    • @RGBA
      @RGBA Год назад

      @@Rytech_Dev Is it possible that you could do a tutorial on how to add animations to that someday?

  • @kalpolproductions8558
    @kalpolproductions8558 Год назад

    your old thumbnail looked better