Hi,
I’m developping a point and clic game and I put my camera inside my character to follow it. But I don’t want the camera to move when he rotates, only when th character goes to the right / left…
Is there a way to do it with C# ?
Hi,
I’m developping a point and clic game and I put my camera inside my character to follow it. But I don’t want the camera to move when he rotates, only when th character goes to the right / left…
Is there a way to do it with C# ?
On your LateUpdate(), you can reset the position and rotation of your camera.
The reason I placed it on LateUpdate instead of Update is because we want the camera to set it’s position/rotation after all the items have updated (specifically the camera’s parent).
Attach this to the camera:
using UnityEngine;
class CameraFollow : MonoBehaviour {
private Quaternion rotation;
private Vector3 position;
void Start() {
rotation = transform.rotation;
position = transform.position;
}
void LateUpdate() {
transform.rotation = rotation;
float x = transform.parent.transform.position.x + position.x;
float y = transform.parent.transform.position.y + position.y;
float z = transform.parent.transform.position.z + position.z;
transform.position = new Vector3(x, y, z);
}
}
In my opinion, I think that you shouldn’t have the camera as a child, because if for any reason your parent item is disabled/destroyed, the camera will be as well.
Thank you very much !!!
It works perfectly !
Have a nice day ![]()