1
votes

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.

1
It's hard to tell what's to blame for the problem because you're using Lerp methods incorrectly. You should use Vector3.MoveTowards and Quaternion.RotateTowards: position = Vector3.MoveTowards(transform.position, new Vector3(newPos.x,newPos.y+cameraHeight,newPos.z), followSpeed * Time.deltaTime); rotation = Quaternion.RotateTowards(transform.rotation, newRot, rotationSpeed * Time.deltaTime); (your rotationSpeed and followSpeed should also be higher)Ruzihm
Is there also a reason why you are using FixedUpdate? You should rather use Update since FixedUpdate is only used for physics related stuff.derHugo
I also noted you are setting the rotation to look into the direction the player moves .. why do you expect it to look at the player then?derHugo
@Ruzihm nope .. sorry I didn't word that well ... they use Lerp because the "smooth" ease-out effect (movement gets slower at the end)derHugo
@Ruzihm Thanks for your comment, Indeed I need some ease in out effect. If you have some better idea, please share :D .LexaGC

1 Answers

2
votes

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)