简体   繁体   中英

Unity C#: Third Person Camera Controller: Snaps when rotating toward camera direction

I am trying to recreate a "GTA-Style" third person camera in Unity. I created code that rotates the player in the direction the camera is pointing. However, the camera is a child of the player, so when I rotate the player, I also rotate the camera. So I have to reset the camera's Y rotation. which in "turn" causes a frame or two to have the camera looking in a different direction.

Hierarchy of Player:

Script:

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

public class ThirdPersonCamera : MonoBehaviour
{
    public float rotationSpeed = 2f;
    public Transform target, player;
    float mouseX;
    float mouseY;
    float h;
    float v;
    
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    
    void LateUpdate()
    {
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        mouseX += Input.GetAxis("Mouse X") * rotationSpeed;
        mouseY -= Input.GetAxis("Mouse Y") * rotationSpeed;
        mouseY = Mathf.Clamp(mouseY, -35, 60);
        CamControl();
    }
    
    void CamControl()
    {
        transform.LookAt(target);
        if (h != 0 || v != 0)
        {
            target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
            player.rotation = Quaternion.Euler(0, mouseX, 0);
        }
        else
        {
            target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
            while (h != 0 || v != 0)
            {
                target.rotation = Quaternion.Euler(target.rotation.x, 0, target.rotation.z);
                player.rotation = Quaternion.Euler(player.rotation.x, player.rotation.y - target.rotation.y + 180, player.rotation.z);
            }
            
        }
    }
}

How this script works: How I have it set up on Main Camera. (Notice hierarchy setup) Note: The "Player" and "Target" variables have the same name as the GameObjects assigned to them. If there is no input on Horizontal and Vertical axes, the camera will rotate around the player like normal. If there is input on the Horizontal and Vertical axes, the player will face the direction the camera is facing. Then it has to reset the camera's Y rotation.

What I have tried:

  1. Using slerp to smooth out the camera angles.

Note: I have TRIED slerping, I'm not sure that I did it correctly.

  1. Unchilding the Target (Camera)

Result: This just made the camera not follow the player, but it shows that my code to turn the player is somewhat functional.

  1. Setting the Player (in script) to the graphics.

Result: This gave me an odd view of the player, and made the player rotate up and down as well.

Sorry for the long and complicated post!

If your code to rotate the player and the camera works when unchilding the target from the player, then don't child the target of camera in the player.
Instead, make the target follow the player.

For instance:

void CamControl()
{
    target.position = player.position;

    transform.LookAt(target);
    ...
}

You can then do a smooth follow using a SmoothDamp for example, instead of copying directly the player position.

Moreover, if your player is controlled by physics (using the rigidbody for the movement), you should update your camera in the fixed update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM