- Home /
Other
How to make camera follow player smoothly on X axes but not on y axis?
Hi guys,
I have a camera script that follows my player smoothly using "transform.position = Vector3.Lerp..." all the time.
Now I put in the scene cannon gun that shoots my player realy fast upwards (only on y axes) and my camera is lazy to follow the player at this point. I have already tryed to increase follow speed or parent the camera to my player but the camera always jumps on the player - that is not what I want.
I want the camera to be parented smoothly to the moving object - to the player. Is it even possible? How should I solve this??
Or even better - to follow my player smoothly on X axes but to be instant on Y axes with smooth aim?
Now I have this (following on X is smooth but the camera jumps on Y axes when targets the player):
float xPos = Mathf.Lerp(transform.position.x, player.transform.position.x, smoothSpeed * Time.deltaTime);
transform.position = new Vector3(xPos, player.transform.position.y, -10);
Please help me...
Answer by DiegoSLTS · Oct 10, 2015 at 09:57 PM
I'm not sure I follow you... Do you want your camera to follow the player in both axis but smooth the camera movement in the x axis only? Or do you only want to follow the player in the X axis and let the y position of the camera untouched?
If it's the first one I think your code should work, so I'm guessing you want to do the second thing. In that case, why are you using the player y position when updating the camera's position? This should follow only in the X axis:
transform.position = new Vector3(xPos, transform.position.y, -10);
Hi, no no. First of all I want my camera to aim my player - to be on the center position + offset of my player smoothly. Then I want my camera to smoohtly follow my player only on the X axes but not smoothly on the Y axes. When the player is fired by the cannon upwards, the camera is lazy to follow him.
It is important to smoothly aim my player because when I parent my camera to the player or increase the follow up speed, the camera suddenly jumps.
Here's my script:
public GameObject player;
public Vector3 offset;
public float smoothSpeed;
void Awake ()
{
transform.position = player.transform.position + offset;
moveUp = player.transform.position.y;
}
void Update()
{
Vector3 moveCamera = new Vector3 (player.transform.position.x, moveUp);
transform.position = Vector3.Lerp (transform.position, moveCamera + offset, smoothSpeed * Time.deltaTime);
}
Never $$anonymous$$d. I just increased camera follow speed and changed Update to FixedUpdate for smoother following.
or :
transform.position = new Vector3(player.transform.position.x, 0, -10);