- Home /
Player not rotating with camera
Hey, so I was writing a mouse look and player movement script to replace the one that comes with unity, but for some reason, the character rotation is not working.
MuseLook Script :
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
private float lookSensitivity = 5f;
private float xRotation;
private float yRotation;
private float currentXRotation;
private float currentYRotation;
private float xRotationV;
private float yRotationV;
private float smoothnessFactor = .1f;
CursorLockMode wantedMode;
// Use this for initialization
void Start () {
wantedMode = CursorLockMode.Confined;
}
public float returnXRot () {
return currentXRotation;
}
public float returnYRot () {
return currentYRotation;
}
// Update is called once per frame
void Update () {
Cursor.lockState = wantedMode;
xRotation -= Input.GetAxis ("Mouse Y") * lookSensitivity;
yRotation += Input.GetAxis ("Mouse X") * lookSensitivity;
xRotation = Mathf.Clamp (xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp (currentXRotation, xRotation, ref xRotationV, smoothnessFactor);
currentYRotation = Mathf.SmoothDamp (currentXRotation, yRotation, ref yRotationV, smoothnessFactor);
transform.rotation = Quaternion.Euler (currentXRotation, currentYRotation, 0);
}
}
PlayerMovement Script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private float speed = 10f;
private float jumpForce = 8f;
private float gravity = 30f;
private Vector3 moveDir = Vector3.zero;
public GameObject playerCamera;
void Start () {
playerCamera = GameObject.Find ("playerCamera");
}
void Update () {
CharacterController controller = gameObject.GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDir = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDir = transform.TransformDirection (moveDir);
moveDir *= speed;
if (Input.GetButtonDown ("Jump")) {
moveDir.y = jumpForce;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move (moveDir * Time.deltaTime);
transform.rotation = Quaternion.Euler (0, playerCamera.transform.rotation.y, 0);
}
}
Any help would be much apreciated
Answer by incorrect · May 01, 2016 at 11:48 PM
I suppose first script is attached to the camera so it affects camera's rotation and does nothing with player object. I'd suggest to use currentYRotation
to rotate player as well. If your camera is a child of player object, you can rotate horizontally the player object instead of a camera.
Thanks! I just set it to rotate my player ins$$anonymous$$d! Now can you tell me why it glitches so badly when i look to the left and right??
wait Never$$anonymous$$d, that didnt rotate the player either
Oh now it works! I just made BoTH of them turn on the Y. Im guessing thats what you meant :/
Actually if your camera is a child of a player object, it will be rotated with player object. But keep in $$anonymous$$d that in this case you'd better use transform.localRotation and transform.localPosition ins$$anonymous$$d.