Unity3d implements third-person movement and camera adjustment

Show the effect first

unity3d

Regarding character movement, it is recommended to use the Character Conrroller component. The advantage is that it does not need to deal with rigid bodies, is not affected by gravity, and has its own physical collision detection.

 Character movement code including steering

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    CharacterController characterController;
    Animator animator;

    private float speed;
    public float walkspeed;
    public float runspeed;
    private bool isrun;

    public Transform cam;
    private Vector3 direction;
    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        animator = GetComponent<Animator>();
    }   
    // Update is called once per frame
    void Update()
    {
        //获取按键
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        if (horizontal!=0||vertical!=0)
        {
            
            animator.SetBool("Walk", true);
        }
        else
        {
            animator.SetBool("Walk", false);
        }
        if(Input.GetKey(KeyCode.LeftShift))
        {
            
            animator.SetBool("Run", true);
        }
        else
        {
            animator.SetBool("Run", false);
        }
        isrun=Input.GetKey(KeyCode.LeftShift);
        if(isrun)
        {
            speed = runspeed;
        }
        else
        {
            speed = walkspeed;
        }
        direction =new Vector3(horizontal,0f,vertical);
        if (direction.magnitude>=0.1f)
        {
            float targetangle=Mathf.Atan2(direction.x,direction.z)*Mathf.Rad2Deg+cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetangle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation=Quaternion.Euler(0f,angle,0f);
            Vector3 moveDir = Quaternion.Euler(0f, targetangle, 0f) * Vector3.forward;
            characterController.Move(moveDir*speed* Time.deltaTime);
        }
        
    }
}
  

The next step is to adjust the camera. Here, we use the plug-in cinemachine that comes with unity, which is highly recommended. You can design a better camera follow-up angle of view without using complicated code.

Installation package:

Toolbar: Window->Package Manager

Just install 

Here we will use FreeLook Camera, and then bind the character:

More important settings:

         TopRig MiddleRig BottomRig These are to set the camera track and basically define the camera. The data in the above picture is an attempt to compare and recommend everyone to try. Be sure to change the Binding Mode to World Space.

You may encounter a situation where the direction of movement of the game player is opposite to the direction of the button. Here you only need to uncheck the red line in the X Axis, and check the Y Axis above.

 Camera completion code (mounted to Main Camera):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThirdPersoncam : MonoBehaviour
{
    public Transform orientation;
    public Transform player;
    public Transform playerObj;

    public float rotationSpeed;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 Dir = player.position - new Vector3(transform.position.x, player.position.y, transform.position.z);
        orientation.forward = Dir.normalized;
        //获取按键
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 inputDir = orientation.forward * vertical + orientation.right * horizontal;
        if (inputDir != Vector3.zero)
        {
            playerObj.forward = Vector3.Slerp(playerObj.forward, inputDir.normalized, Time.deltaTime * rotationSpeed);
        }
    }
}

The playerobj and orientation here are sub-empty objects mounted on the game character to assist the orientation.

Guess you like

Origin blog.csdn.net/m0_64652083/article/details/128794018