- Home /
Question by
Trick_Shot_Magic · Aug 28, 2017 at 10:07 PM ·
playerrotatecamera rotate
how do i rotate the player with the camera using this code i made.
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX = 50.0f;
public Quaternion rotacion = Quaternion.identity;
public Transform lookat;
public Transform camtransform;
private Camera cam;
public float distance = 40.0f;
public float currentX = 0.0f;
public float currentY = 0.0f;
public float sensivityx = 4.0f;
public float sensivityy = 4.0f;
// Use this for initialization
private void Start () {
camtransform = transform;
cam = Camera.main;
}
private void Update () {
currentX += Input.GetAxis ("Mouse X");
currentY -= Input.GetAxis ("Mouse Y");
currentY = Mathf.Clamp (currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
void LateUpdate () {
Vector3 dir = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
camtransform.position = lookat.position + rotation * dir;
camtransform.LookAt (lookat.position);
}
Comment
Answer by pslattery · Aug 29, 2017 at 12:56 AM
You could add a public field like
GameObject playerObject;
that you set in the editor. Then in Update, just call
playerObject.transform.Rotate (0, Input.GetAxis("Mouse X"), 0);
Another way to do all this is to just rotate your Player object in its own script (basically the same as what I wrote above), and use the new Cinemachine system to handle the camera. Its a free download and there's even a Camera behavior for this exact thing.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers
How to make my camera only rotate on middle mouse click? 1 Answer
is it possible to have your camerarotation be *-1 of you player rotation. 1 Answer