unity3d 实现第三人称移动与摄像机调整

首先展示效果

unity3d

关于人物移动,这里推荐使用Character Conrroller组件,优点就是可以不用处理刚体,不受重力的影响,自带物理碰撞检测。

 人物移动代码包括转向

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);
        }
        
    }
}
  

接下来就是摄影机的调整,这里使用unity自带的插件cinemachine,非常的推荐,无需使用冗杂的代码即可设计较好的摄像机跟随视角。

安装包:

工具栏:Window->Package Manager

安装即可 

这里我们将使用FreeLook Camera,然后绑定角色:

比较重要的设置:

         TopRig MiddleRig BottomRig 这些是设置相机轨道也就基本定义了相机,上图数据构成的试图比较推荐大家试试,一定要记得改Binding Mode为 World Space。

大家可能会遇到游戏玩家的运动方向与按键方向相反的状况,这里只需要将X Axis 中画红线部分取消勾选,上方Y Axis 勾选即可

 摄影机完成代码(挂载到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);
        }
    }
}

此处的playerobj以及orientation是挂载在游戏角色上的子空物体,用来辅助方向。

猜你喜欢

转载自blog.csdn.net/m0_64652083/article/details/128794018
今日推荐