Unity 3D character movement direction follows the camera (first person)

principle

Use the character's body rotation to control the camera's left and right slides,
and the camera's rotation to control the camera's up and down slides.

Detailed analysis

First of all, we need to know that the camera is a sub-object of the character (acting as an eye),
so when the character rotates, the camera will follow it and rotate in the same direction and the same angle,
so that the direction in which the eye (camera) looks must be directly in front of the character.

So what are the benefits of doing this?
If the camera lens is locked to the front of the character,
controlling the character's movement direction will become very precise.

If you only use the camera to control the up, down, left and right sliding of the lens,
although this can achieve a full first perspective
, but when the character moves, there will be a direction error
, because although the camera direction changes, the character's direction does not change.
For example: You Turn the camera 90 degrees to the left, press W and prepare to move forward, but find that it has moved to the right (front of the initial direction)
Insert image description here

Therefore, if you want to move in an accurate direction, the camera orientation must be consistent with the character's orientation
Insert image description here
* Pay attention to distinguishing the orientation of the characters and the camera in the two pictures.

Code explanation

core part

//俯仰
//以摄像机的旋转控制镜头上下滑动
void Pitch()
{
    
    
  float pitchAngle = Input.GetAxis("Mouse Y") * pitchSensitivity * Time.deltaTime;
  Quaternion pitch = Quaternion.AngleAxis(pitchAngle, -Vector3.right); //绕X轴负方向旋转的度数
  Quaternion finalAngle = playerCam.localRotation * pitch; //欧拉角转化为四元数 使摄像机旋转

  //限定俯仰的角度
  if (Quaternion.Angle(playerCamCenter, finalAngle) < pitchLimit)
  {
    
    
    playerCam.localRotation = finalAngle;//赋值
  }
}

//偏转
//以人物的旋转控制镜头左右滑动
void Yaw()
{
    
    
  float yawAngle = Input.GetAxis("Mouse X") * yawSensitivity * Time.deltaTime;
  Quaternion yaw = Quaternion.AngleAxis(yawAngle, Vector3.up); //绕Y轴旋转的度数
  Quaternion finalAngle = player.localRotation * yaw; //欧拉角转化为四元数 使人物旋转

  player.localRotation =finalAngle;//赋值
}

Detailed analysis

Get people and cameras

public Transform player;
public Transform playerCam;

Set the sensitivity of pitch and deflection (details of pitch and deflection are explained in the previous article)

public float pitchSensitivity; //Y方向 俯仰(绕X轴)
public float yawSensitivity; //X方向 偏转(绕Y轴)

Limit the maximum pitch angle (no restrictions on deflection in the horizontal direction)

public float pitchLimit = 75;

Calculate rotation angle

float pitchAngle = Input.GetAxis("Mouse Y") * pitchSensitivity * Time.deltaTime;
float yawAngle = Input.GetAxis("Mouse X") * yawSensitivity * Time.deltaTime;

Rotate around a specified axis

Quaternion pitch = Quaternion.AngleAxis(pitchAngle, -Vector3.right);//绕X轴负方向旋转的度数
Quaternion yaw = Quaternion.AngleAxis(yawAngle, Vector3.up); //绕Y轴旋转的度数

Understanding Quaternion.AngleAxis

Quaternion: Quaternion is basically used in Unity to calculate orientation and rotation.
AngleAxis: an axis around which
the whole thing is: specify an axis, rotate the specified angle around the axis, and obtain a new rotation result.

targetRotation = Quaternion.AngleAxis(angel, direction);

angle: represents how many degrees of rotation
direction: which direction around which axis (such as Vector3.up, Vector3.right, etc.)

Convert Euler angles to quaternion

Quaternion finalAngle = playerCam.localRotation * pitch;
Quaternion finalAngle = player.localRotation * yaw;

Complete final assignment
camera

if (Quaternion.Angle(playerCamCenter, finalAngle) < pitchLimit)
        {
    
    
            playerCam.localRotation = finalAngle;
        }

figure

player.localRotation =finalAngle;

Complete code

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

public class PlayerLookControl : MonoBehaviour
{
    
    
    //获取摄像机和人物
    public Transform player;
    public Transform playerCam;

    //俯仰和偏转的灵敏度
    public float pitchSensitivity; //Y方向 俯仰(绕X轴)
    public float yawSensitivity; //X方向 偏转(绕Y轴)

    //最大俯仰角度
    public float pitchLimit = 75;

    private Quaternion playerCamCenter;

    void Start()
    {
    
    
        playerCamCenter = playerCam.localRotation;
        Cursor.lockState = CursorLockMode.Locked; //将鼠标光标锁定在窗口中心
        Cursor.visible = false; //隐藏鼠标光标
    }

    void Update()
    {
    
    
        Pitch();
        Yaw();
    }

    //俯仰
    //以摄像机的旋转控制镜头上下滑动
    void Pitch()
    {
    
    
        float pitchAngle = Input.GetAxis("Mouse Y") * pitchSensitivity * Time.deltaTime;
        Quaternion pitch = Quaternion.AngleAxis(pitchAngle, -Vector3.right); //绕X轴负方向旋转的度数
        Quaternion finalAngle = playerCam.localRotation * pitch; //欧拉角转化为四元数 使摄像机旋转

        //限定俯仰的角度
        if (Quaternion.Angle(playerCamCenter, finalAngle) < pitchLimit)
        {
    
    
            playerCam.localRotation = finalAngle;//赋值
        }
    }

    //偏转
    //以人物的旋转控制镜头左右滑动
    void Yaw()
    {
    
    
        float yawAngle = Input.GetAxis("Mouse X") * yawSensitivity * Time.deltaTime;
        Quaternion yaw = Quaternion.AngleAxis(yawAngle, Vector3.up); //绕Y轴旋转的度数
        Quaternion finalAngle = player.localRotation * yaw; //欧拉角转化为四元数 使人物旋转

        player.localRotation =finalAngle;//赋值
    }
}

Guess you like

Origin blog.csdn.net/m0_73241844/article/details/132445802