Question by
jmgarneau451 · May 24, 2020 at 09:32 PM ·
c#camerainput3dcontroller
Xbox one controller spinning
I am making an fps game and I want to map my Xbox one controller so that I can use it in my game. All of the buttons and the left joystick work. But, when I try to map the right joystick and play the game my camera looks up and my player starts spinning around. Here is the code for my camera:
public string mouseXinputname, mouseYinputname;
public float mouseSensitivity;
private float Xaxisclamp;
[SerializeField] private Transform playerBody;
private void Awake()
{
LockCursor();
Xaxisclamp = 0.00f;
}
private void LockCursor ()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
CameraRotaion();
}
private void CameraRotaion ()
{
float mouseX = Input.GetAxis(mouseXinputname) * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis(mouseYinputname) * mouseSensitivity * Time.deltaTime;
transform.Rotate(Vector3.left * mouseY);
playerBody.Rotate(Vector3.up * mouseX);
Xaxisclamp += mouseY;
if(Xaxisclamp > 90.0f)
{
Xaxisclamp = 90.0f;
mouseY = 0.0f;
ClampXaxisRotationToValue(270.0f);
}
else if (Xaxisclamp < -90.0f)
{
Xaxisclamp = -90.0f;
mouseY = 0.0f;
ClampXaxisRotationToValue(90.0f);
}
}
private void ClampXaxisRotationToValue(float value)
{
Vector3 eulerRotation = transform.eulerAngles;
eulerRotation.x = value;
transform.eulerAngles = eulerRotation;
}
And here is the code for my player controller:
public CharacterController character;
public float speed = 10f;
Vector3 velocity;
public float gravtiy = -9.81f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
public float jumpHeight = 3f;
public float sprint = 20f;
public float crouchHeight = 1.0f;
public float crouchSpeed = 5f;
private void Start()
{
character.GetComponent<CharacterController>();
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0 )
{
velocity.y = -2.0f;
}
if (Input.GetButtonDown("Sprint"))
{
speed = sprint;
}
if (Input.GetButtonUp("Sprint"))
{
speed = 10f;
}
if (Input.GetButtonDown("Crouch"))
{
character.height = crouchHeight;
speed = crouchSpeed;
}
if (Input.GetButtonUp("Crouch"))
{
character.height = 2f;
speed = 10f;
}
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 move = transform.right * hor + transform.forward * ver;
character.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravtiy);
}
velocity.y += gravtiy * Time.deltaTime;
character.Move(velocity * Time.deltaTime);
}
I included all of my code so you can copy and paste it into your own unity project so you can see what I mean.
BTW: The inputs "Crouch" and "Sprint" are custom inputs and you'll have to create them in Project Settings > Input Manager
Comment