i am tying to build a fps controller from scratch and cant seem to get him to look up or down
[RequireComponent(typeof(Rigidbody))] public class Movement : MonoBehaviour { [SerializeField] private Camera Cam;
Rigidbody rb;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
//private Vector3 camreaRotation = Vector3.zero;
private float camreaRotationX = 0f;
private float currentcamreaRotationX = 0f;
[SerializeField]
private float cameraRotationLimit = 85f;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
// Gets a movement vector
public void Mov(Vector3 _velocity)
{
velocity = _velocity;
}
//get a rotational vector
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
//get a rotational vector for cam
public void RotateCamera(float _camreaRotationX)
{
camreaRotationX = _camreaRotationX;
}
//runs all Physics
private void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
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)
{
// Set our rotation and clamp it
currentcamreaRotationX -= camreaRotationX;
currentcamreaRotationX = Mathf.Clamp(currentcamreaRotationX, -cameraRotationLimit, cameraRotationLimit);
//Apply our rotation to the transform of our camera
Cam.transform.localEulerAngles = new Vector3(currentcamreaRotationX, 0f, 0f);
//Cam.transform.Rotate(-camreaRotation);
}
}
}
Player Controller
[RequireComponent(typeof(Movement))] public class PlayerController : MonoBehaviour { [SerializeField] private float Movespeed = 10.0f; [SerializeField] private float LookSens = 10.0f;
private Movement Motor;
private void Start()
{
Motor = GetComponent<Movement>();
}
private void Update()
{
//lock cursor to center of screen press Esc to escape
Cursor.lockState = CursorLockMode.Locked;
//calculate movement Velocity as 3D vector
float xMov = Input.GetAxisRaw("Horizontal");
float zmov = Input.GetAxisRaw("Vertical");
Vector3 MovHoriz = transform.right * xMov;
Vector3 MovVert = transform.forward * zmov;
// final movement Vector
Vector3 Velocity = (MovHoriz + MovVert).normalized * Movespeed;
//Apply Movement
Motor.Mov(Velocity);
//Calculate rotation as 3D Vector (turning arround)
float _yrot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yrot, 0f) * LookSens;
//Apply rotation
Motor.Rotate(_rotation);
//Calculate camrea rotation as 3D Vector (turning arround)
float _xrot = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xrot * LookSens;
//Vector3 _camrearotation = new Vector3(0f, _xrot, 0f) * LookSens;
//Apply rotation
Motor.RotateCamera(_cameraRotationX);
}
}
,[RequireComponent(typeof(Rigidbody))] public class Movement : MonoBehaviour { [SerializeField] private Camera Cam;
Rigidbody rb;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
//private Vector3 camreaRotation = Vector3.zero;
private float camreaRotationX = 0f;
private float currentcamreaRotationX = 0f;
[SerializeField]
private float cameraRotationLimit = 85f;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
// Gets a movement vector
public void Mov(Vector3 _velocity)
{
velocity = _velocity;
}
//get a rotational vector
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
//get a rotational vector for cam
public void RotateCamera(float _camreaRotationX)
{
camreaRotationX = _camreaRotationX;
}
//runs all Physics
private void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
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)
{
// Set our rotation and clamp it
currentcamreaRotationX -= camreaRotationX;
currentcamreaRotationX = Mathf.Clamp(currentcamreaRotationX, -cameraRotationLimit, cameraRotationLimit);
//Apply our rotation to the transform of our camera
Cam.transform.localEulerAngles = new Vector3(currentcamreaRotationX, 0f, 0f);
//Cam.transform.Rotate(-camreaRotation);
}
}
}
player controller
[RequireComponent(typeof(Movement))] public class PlayerController : MonoBehaviour { [SerializeField] private float Movespeed = 10.0f; [SerializeField] private float LookSens = 10.0f;
private Movement Motor;
private void Start()
{
Motor = GetComponent<Movement>();
}
private void Update()
{
//lock cursor to center of screen press Esc to escape
Cursor.lockState = CursorLockMode.Locked;
//calculate movement Velocity as 3D vector
float xMov = Input.GetAxisRaw("Horizontal");
float zmov = Input.GetAxisRaw("Vertical");
Vector3 MovHoriz = transform.right * xMov;
Vector3 MovVert = transform.forward * zmov;
// final movement Vector
Vector3 Velocity = (MovHoriz + MovVert).normalized * Movespeed;
//Apply Movement
Motor.Mov(Velocity);
//Calculate rotation as 3D Vector (turning arround)
float _yrot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yrot, 0f) * LookSens;
//Apply rotation
Motor.Rotate(_rotation);
//Calculate camrea rotation as 3D Vector (turning arround)
float _xrot = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xrot * LookSens;
//Vector3 _camrearotation = new Vector3(0f, _xrot, 0f) * LookSens;
//Apply rotation
Motor.RotateCamera(_cameraRotationX);
}
}
Your answer
Follow this Question
Related Questions
Tilt camera left or right on keyboard input,Tilt camera when player moves left or right 0 Answers
Camera spawn rotate with object 0 Answers
How do I do my camera player look at an specific direction when key pressed 0 Answers
How to configure my camera ?? 0 Answers
(NEED HELP!) Camera controlled by mouse Y, player controlled by mouse X 2 Answers