Hi I have recently come across a problem i have written code for a player to move and to look around but my camera is suppose to look up and down but it doesnt i have attached the camera to the script and it doesnt work here is the code
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3f;
private PlayerMotor motor;
void Start()
{
motor = GetComponent<PlayerMotor>();
}
void Update ()
{
//calculate movement velocity as 3d vector
float _xMov = Input.GetAxisRaw ("Horizontal");
float _zMov = Input.GetAxisRaw ("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
motor.Move(_velocity);
//calculatrerotain3dvector this applys ti turniung
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
motor.Rotate(_rotation);
//calculatrerotain3dvector this applys ti turniung
float _xRot = Input.GetAxisRaw("Mouse Y");
Vector3 _cameraRotation = new Vector3 (0f, _xRot, 0f) * lookSensitivity;
motor.RotateCamera(_cameraRotation);
}
}
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
[SerializeField]
private Camera cam;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 cameraRotation = Vector3.zero;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Move (Vector3 _velocity)
{
velocity = _velocity;
}
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
public void RotateCamera(Vector3 _cameraRotation)
{
cameraRotation = _cameraRotation;
}
void FixedUpdate ()
{
PerformMovement();
PerformRotation();
}
//preform movement
void PerformMovement()
{
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
void PerformRotation()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
if (cam != null)
{
cam.transform.Rotate (-cameraRotation);
}
}
}