LexaGC
LexaGC

Reputation: 130

Rotate Camera vector to look at player Unity

I create a game, like minigolf/pool. I want to have a camera which follow player.

Position is normally ok, I get the ball direction and I lerp. Rotation is almost ok. Currently, rotation by Y axis is ok, but camera look straigth forward, and don't look down to the player : enter image description here

enter image description here

I already try many thing , quaternion angleToaxis, quarternion lookat ... but doesn't look good, the camera go look away ... Here my code

namespace CameraManagerNameSpace
{
    public class CameraManager : MonoBehaviour 
    {
        public float cameraHeight=13f;
        public PlayerNameSpace.Player playerToFollow; 
        public float followSpeed = 3f;
        public float rotationSpeed = 1f;

        float distance;
        Vector3 position;
        Vector3 newPos;
        Quaternion rotation;
        Quaternion newRot;
        Vector3 playerPrevPos, playerMoveDir;
        bool firstMoveDone=false;

        void Start() 
        {
            playerPrevPos = playerToFollow.player_transform.position;
            distance = Vector3.Distance(transform.position,playerToFollow.player_transform.position);
        }

        void FixedUpdate() 
        {
            if(Vector3.Distance(playerToFollow.player_transform.position ,playerPrevPos)>0.5f || firstMoveDone)
            {
                playerMoveDir = playerToFollow.player_transform.position - playerPrevPos;
                firstMoveDone = true;
            }
            else 
            {
                playerMoveDir = new Vector3(0,0,0);
            }

            if (playerMoveDir != Vector3.zero)
            {
                playerMoveDir.Normalize();

                newPos = playerToFollow.player_transform.position - playerMoveDir * distance;
                newRot =Quaternion.LookRotation(playerMoveDir,Vector3.up);

                position = Vector3.Lerp(transform.position, new Vector3(newPos.x,newPos.y+cameraHeight,newPos.z), followSpeed * Time.deltaTime);
                rotation = Quaternion.Lerp(transform.rotation, newRot, rotationSpeed * Time.deltaTime);

                transform.position = position;
                transform.rotation = rotation;

                playerPrevPos = playerToFollow.player_transform.position;
            }
        }
    }
}

Also, I don't know why, but after the balls stop the camera continue to do some movement very jerky, jolting, halting.

Upvotes: 1

Views: 2908

Answers (1)

derHugo
derHugo

Reputation: 90580

Well in

newRot = Quaternion.LookRotation(playerMoveDir,Vector3.up);

you are saying the camera to look in the same direction the player is moving ... not to look at the player. This would work if you wouldn't give the camera an extra position offset in the Y axis.


You might want to rather try

// vector pointing from the camera towards the player
var targetDirection = playerToFollow.player_transform.position - transform.position;
newRot = Quaternion.LookRotation(targetDirection, Vector3.up);

You should also rather use Update since FixedUpdate is only used for physics related stuff (also see Update & FixedUpdate)

Upvotes: 2

Related Questions