I’m currently just practising making basic scripts. I made two separate scripts for the camera and a player to make them look on separate axis. The camera looks on the Y axis whilst the player looks on the X axis. This is to stop the player object from lying on a 90 degree angle. I also set the camera to be a child object of the player so that it’ll look on the X axis with the player. The problem is that when I move my mouse to rotate the player it stops when the mouse stops but the camera continues spinning until I move the mouse in the opposite direction to cancel the rotation.
Here’s the code for the player object:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public int speed;
public float turnSpeed;
private Vector3 movement;
private Quaternion turn;
Rigidbody playerRB;
// Use this for initialization
void Start () {
playerRB = GetComponent<Rigidbody> ();
playerRB.freezeRotation = true;
}
// Update is called once per frame
void Update () {
movement = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
transform.Translate (movement * Time.deltaTime * speed);
turn.eulerAngles = new Vector3 (0.0f, Input.GetAxis ("Mouse X"), 0.0f);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp (current, current * turn, Time.deltaTime * turnSpeed);
}
}
And this is the cameras:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
private Quaternion turn;
private GameObject player;
private float turnSpeed;
// Use this for initialization
void Start () {
turnSpeed = GameObject.Find ("Jeff").GetComponent<PlayerController> ().turnSpeed;
}
// Update is called once per frame
void Update () {
turn.eulerAngles = new Vector3 (Input.GetAxis ("Mouse Y"), 0.0f, 0.0f);
transform.localRotation = Quaternion.Slerp (transform.localRotation, transform.rotation * turn, Time.deltaTime * turnSpeed);
transform.localRotation.x = transform.localRotation.x;
}
}
Anyone know how to fix it?