Hello,
I have a 3rd-person camera that isnt a a parent or child of my player.
My camera is orbiting around my player and my player is always facing in camera direction with this script:
void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Math.Clamp(currentY, MIN_Y, MAX_Y);
distance -+ Input.GetAxis("Mouse ScrollWheel");
}
void LateUpdate()
{
Vector3 dir = new Vector3(0f, 0f, -distance);
Vector3 h = new Vector3(0f, height, 0f);
Quaternion rotation = Quaternion.Euler(-currentY, currentX, 0);
transform.position = lookAt.position + h + rotation * dir;
transform.LookAt(lookAt.position + h);
GameObject.FindGameObjectWithTag("Player").transform.rotation = transform.rotation;
}
This works perfectly fine.
But when i try to adjust my lookAt-point 3 X-Units in players local direction my player starts to jitter as fuck while moving the camera.
I manipulated the above code like:
void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Math.Clamp(currentY, MIN_Y, MAX_Y);
distance -+ Input.GetAxis("Mouse ScrollWheel");
}
void LateUpdate()
{
Vector3 dir = new Vector3(0f, 0f, -distance);
Vector3 h = new Vector3(0f, height, 0f);
Quaternion rotation = Quaternion.Euler(-currentY, currentX, 0);
transform.position = (lookAt.position + lookAt.transform.TransformDirection(Vector3.right * 3)) + h + rotation * dir;
transform.LookAt((lookAt.position + lookAt.transform.TransformDirection(Vector3.right * 3)) + h);
GameObject.FindGameObjectWithTag("Player").transform.rotation = transform.rotation;
}
I dont know why everything is fucked up when i try to relocate the lookAt-point.
PS: I removed the PlayerController, so that only my camerascript transforms/rotates my player for testing.
Any ideas to stop the jittering?
Hi there,
It would be helpful if you could post how lookAt.position is derived, it may be a common third party script but I’m not aware of where this is coming from.
I think there might be some fighting going on between this position and your transform.LookAt statement if the lookAt.position is derived by the direction the camera is facing.
If I’ve understood you correctly, you are looking for a camera system that orbits the player but also constantly looks at a point 3 units to the right of the player.
I would break down this problem further, effectively you are trying to do two things at once, a circular rotation and a modification of the cameras forward vector (where its looking).
I would advise simplifying this problem into two parts:
Create a game object with a new script (can be a cube for now for easier testing) and just make it orbit the players transform.position. Preferably only using rotation and not lookat (which you have already as you use it to rotate the player).
Then make the camera a child of this object, which means it will rotate with the parent. Then all you need is a new script on the camera that all it does is transform.Lookat(playerTransform.position + playerTransform.right *3); You can make the camera look any which way you want and never have to worry about disturbing the smoothness of the orbit.
Kind Regards,
Joe
Ok i figured out a workaround:
At first you set the lookAt-Point (Player) without modifications to its position.
After you have done that, you Translate the camera.
void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Math.Clamp(currentY, MIN_Y, MAX_Y);
distance -+ Input.GetAxis("Mouse ScrollWheel");
}
void LateUpdate()
{
Vector3 dir = new Vector3(0f, 0f, -distance);
Vector3 h = new Vector3(0f, height, 0f);
Quaternion rotation = Quaternion.Euler(-currentY, currentX, 0);
transform.position = lookAt.position + h + rotation * dir;
transform.LookAt(lookAt.position + h);
// Move the camera
transform.Translate(Vector3.right * 3);
GameObject.FindGameObjectWithTag("Player").transform.rotation = transform.rotation;
}
It seems there is a problem using
lookAt.transform.TransformDirection(Vector3.right * 3)
1 Like