I can't look up or down with a custom FPS Controller.,I cant move my FPS camera up or down with this custom script.
Due to the FPS Controller seeming to be broken in Unity now, I started using a custom one by kinifi on GitHub. For some reason though I can't look up or down. I removed the Main Camera so it should of fixed it but no. Is there anything I can change in this script to allow me to look up and down.
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class FPS : MonoBehaviour {
private float speed = 5.0f;
private float m_MovX;
private float m_MovY;
private Vector3 m_moveHorizontal;
private Vector3 m_movVertical;
private Vector3 m_velocity;
private Rigidbody m_Rigid;
private float m_yRot;
private float m_xRot;
private Vector3 m_rotation;
private Vector3 m_cameraRotation;
private float m_lookSensitivity = 3.0f;
private bool m_cursorIsLocked = true;
[Header("The Camera the player looks through")]
public Camera m_Camera;
// Use this for initialization
private void Start()
{
m_Rigid = GetComponent<Rigidbody>();
}
// Update is called once per frame
public void Update()
{
m_MovX = Input.GetAxis("Horizontal");
m_MovY = Input.GetAxis("Vertical");
m_moveHorizontal = transform.right * m_MovX;
m_movVertical = transform.forward * m_MovY;
m_velocity = (m_moveHorizontal + m_movVertical).normalized * speed;
//mouse movement
m_yRot = Input.GetAxisRaw("Mouse X");
m_rotation = new Vector3(0, m_yRot, 0) * m_lookSensitivity;
m_xRot = Input.GetAxisRaw("Mouse Y");
m_cameraRotation = new Vector3(m_xRot, 0, 0) * m_lookSensitivity;
//apply camera rotation
//move the actual player here
if (m_velocity != Vector3.zero)
{
m_Rigid.MovePosition(m_Rigid.position + m_velocity * Time.fixedDeltaTime);
}
if (m_rotation != Vector3.zero)
{
//rotate the camera of the player
m_Rigid.MoveRotation(m_Rigid.rotation * Quaternion.Euler(m_rotation));
}
if (m_Camera != null)
{
//negate this value so it rotates like a FPS not like a plane
m_Camera.transform.Rotate(-m_cameraRotation);
}
InternalLockUpdate();
}
//controls the locking and unlocking of the mouse
private void InternalLockUpdate()
{
if (Input.GetKeyUp(KeyCode.Escape))
{
m_cursorIsLocked = false;
}
else if (Input.GetMouseButtonUp(0))
{
m_cursorIsLocked = true;
}
if (m_cursorIsLocked)
{
UnlockCursor();
}
else if (!m_cursorIsLocked)
{
LockCursor();
}
}
private void UnlockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void LockCursor()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
} ,Due to the Unity FPS Controller seeming to be broken in Unity now, I am using a custom one by kinifi on GitHub. However I can't look up or down with it. Is there anything I can do to solve this issue, here's the script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class FPS : MonoBehaviour {
private float speed = 5.0f;
private float m_MovX;
private float m_MovY;
private Vector3 m_moveHorizontal;
private Vector3 m_movVertical;
private Vector3 m_velocity;
private Rigidbody m_Rigid;
private float m_yRot;
private float m_xRot;
private Vector3 m_rotation;
private Vector3 m_cameraRotation;
private float m_lookSensitivity = 3.0f;
private bool m_cursorIsLocked = true;
[Header("The Camera the player looks through")]
public Camera m_Camera;
// Use this for initialization
private void Start()
{
m_Rigid = GetComponent<Rigidbody>();
}
// Update is called once per frame
public void Update()
{
m_MovX = Input.GetAxis("Horizontal");
m_MovY = Input.GetAxis("Vertical");
m_moveHorizontal = transform.right * m_MovX;
m_movVertical = transform.forward * m_MovY;
m_velocity = (m_moveHorizontal + m_movVertical).normalized * speed;
//mouse movement
m_yRot = Input.GetAxisRaw("Mouse X");
m_rotation = new Vector3(0, m_yRot, 0) * m_lookSensitivity;
m_xRot = Input.GetAxisRaw("Mouse Y");
m_cameraRotation = new Vector3(m_xRot, 0, 0) * m_lookSensitivity;
//apply camera rotation
//move the actual player here
if (m_velocity != Vector3.zero)
{
m_Rigid.MovePosition(m_Rigid.position + m_velocity * Time.fixedDeltaTime);
}
if (m_rotation != Vector3.zero)
{
//rotate the camera of the player
m_Rigid.MoveRotation(m_Rigid.rotation * Quaternion.Euler(m_rotation));
}
if (m_Camera != null)
{
//negate this value so it rotates like a FPS not like a plane
m_Camera.transform.Rotate(-m_cameraRotation);
}
InternalLockUpdate();
}
//controls the locking and unlocking of the mouse
private void InternalLockUpdate()
{
if (Input.GetKeyUp(KeyCode.Escape))
{
m_cursorIsLocked = false;
}
else if (Input.GetMouseButtonUp(0))
{
m_cursorIsLocked = true;
}
if (m_cursorIsLocked)
{
UnlockCursor();
}
else if (!m_cursorIsLocked)
{
LockCursor();
}
}
private void UnlockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void LockCursor()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}