- Home /
Viewmodel/camera rotation when moving
I am creating a first person platformer where the player has full body awareness. The player uses hover skates to move around the map, and I can't figure out how to get the first person body to rotate to simulate the player leaning forward, backwards, etc., as well as the camera rotating left and right when strafing.
Could someone explain how to do this?
Answer by Quillicit · Jul 04, 2013 at 01:06 PM
I have a camera as a child of my player, and use this script to control it:
public class CameraFollow : MonoBehaviour {
public float rotspeed = 10F;
public float camUD = 0F;
float rotYold = 0;
float rotYnew = 0;
void Update () {
transform.position = transform.parent.position + new Vector3 (0, 1, 0); // Keeps camera 1 unit above player
camUD += Input.GetAxis("Mouse Y") * rotspeed * Time.deltaTime; // Vertical rotation controlled by mouse up/down
rotYnew = transform.parent.eulerAngles.y; // Target horizontal rotation matches player horizontal rotation
rotYold = transform.eulerAngles.y; // Get current horizontal rotation
transform.Rotate(0, rotYnew - rotYold, 0); // Rotate camera horizontally
camUD = Mathf.Clamp (camUD, -60, 60); // Camera can't rotate vertically farther than 60 degrees
transform.rotation = Quaternion.Euler(-camUD, transform.eulerAngles.y, 0); // Rotate camera vertically
}
}
You should be able to modify this for rotating your player, but let me know if you need help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make the camera rotate only when right click is held down on the mouse? 1 Answer
Why is my camera switching angles? 0 Answers
Camera follows target rotation 3 Answers
Limiting the rotation to 180 degrees 0 Answers