- Home /
Why is my camera's y-position slowly decreasing as my character moves?
I'm trying to get my camera to follow the player and rotate with it. The camera does follow and rotate with the player, but as the player continues to move forward the camera's y-position is slowly decreasing until it reaches the ground. The camera will then be stuck in that position.
private Transform target;
void Start()
{
target = GameObject.FindWithTag("Player").transform;
}
void LateUpdate()
{
transform.position = Vector3.Lerp(transform.position, target.position + (transform.position - target.position).normalized * 10, Time.deltaTime * 5);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X") * 4.0f);
transform.LookAt(target);
}
Sorry, if I didn't adequately explain the problem or my question is a bit confusing to understand. I also apologize if my code is a bit....unorthodox.
unless you want the camera to move from it's current position to player position you don't need Vector3.Lerp
or Vector3.movetowards
you can use :
transform.position = new Vector3(target.position.x,target.position.y,-10);
you can add offset to X,Y & -10 is the default z position of the camera . & if it's not necessary you can avoid target = GameObject.FindWithTag("Player").transform;
:
public Transform target; // attach the targit game object to this slot .
I originally was using new Vector3(), but the reason why I switched to Vector3.Lerp, was because each time I created that new vector and used transform.LookAt() or transform. RotateAround(), I woud not be able to rotate the camera one bit.
is it only the Y not the X & Z also. & have you tried to delete transform.RotateAround
& transform.LookAt
& see if the problem still so we focus on
target.position + (transform.position - target.position).normalized * 10
Answer by brunocoimbra · Apr 19, 2016 at 11:13 PM
transform.position = Vector3.Lerp(transform.position, target.position + (transform.position - target.position).normalized * 10, Time.deltaTime * 5);
You are using Lerp in a very wrong way!
First: target.position + (transform.position - target.position).normalized * 10
It says "Pick my target.position and add 10 times the value of the direction between the transform and the target." So as the target's trasform is at the ground, and the camera ia above it, it will keep re-calculating it's way all to the ground.
Second: Time.deltaTime * 5
Yeah, it is the Unity's sample about how to use the Lerp, but it is wrong, Lerp isn't meant to be used with Time.deltaTime.
As others said, use Vector3.MoveTowards(transform.position, target.position, maxDistanceDelta);
Thank you everyone, who has assisted me in fixing this issue. I appreciated the assistance.
Answer by chetanmac · Apr 19, 2016 at 07:13 AM
You Should use vector3.movetowards instead of vector3.lerp.
It slowed down the camera from falling, but it ultimately still falls.
Answer by KaushikRahul · Apr 19, 2016 at 06:03 AM
transform.position = Vector3.Lerp(transform.position, target.position + (transform.position - target.position).normalized * 10, Time.deltaTime * 5);
Here what you are trying to do is Lerping position of the the camera from its own position to the target's position. So for the player, the pivot is going to be at the bottom which in turn results makes the camera lerp to the ground.
Instead of using the script, what you can do is, you can directly attach the camera to the player by making the camera child of the player.
I did that at first, but the animations for my character causes some rotation when he walks forward, therefore my camera will also rotate when he walks forward.
Okey do one thing.
Create an empty object and make both, your character and the camera child of it.
then add the movement scripts to this game object ins$$anonymous$$d of adding those to the character.
this way you character will move, your camera will follow it around and also your animations wont affect the camera.
:) :)
Unfortunately, I'm using root motion, so the animation is doing all the movement. On my last character, that did work though since I was using transform.Translate() to move my character.
Answer by Ryanless · Apr 19, 2016 at 06:50 PM
u have to fix the distance between the camera and the player.
something like the camera script here: yourCameraTutorial
Its a Tutorial which explaines how to make exactly a script how you like it ( at least i gess its what you want). There is also already the code there just to copy-paste.