Question by
robert97mc · Jul 08, 2019 at 04:10 PM ·
rotationcharacter
Character won't rotate along with camera on y axis
So I have "PlayerMovementScript" attached to player and "MouseLookScript" attached to player camera. So when I rotate my camera using mouse (FPS) I want my character to rotate around Y axis along with my mouse but I can't see where is the problem.
Here is MouseLookScript:
using UnityEngine;
public class MouseLookScript : MonoBehaviour
{
public float mouseSensitivity = 2.5f;
private void Update()
{
float xRotation =- Input.GetAxis("Mouse Y");
float yRotation =+ Input.GetAxis("Mouse X");
transform.eulerAngles += new Vector3(xRotation, yRotation, 0) * mouseSensitivity;
}
}
and here is PlayerMovementScript:
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
public float movementSpeed = 10f;
public GameObject playerCamera;
private void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (rb == null)
gameObject.AddComponent<Rigidbody>();
else
return;
}
private void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed;
float moveVertical = Input.GetAxis("Vertical") * Time.deltaTime * movementSpeed;
transform.position += new Vector3(moveHorizontal, 0, moveVertical);
transform.rotation = Quaternion.Euler(0, playerCamera.transform.rotation.y, 0);
}
}
Comment
Your answer
Follow this Question
Related Questions
Quaternion.LookAt() without restricting manual rotation 2 Answers
How To Lock Direction While Jumping ? 1 Answer
Character Rotation 0 Answers