USB Controller Axis overriding mouse!
Basically, I have this script that sets up the first person character's camera. A while ago I created it to take a Vector3 of the mouse x and y and rotate the camera based off of those numbers. It worked great. So, now that I am adding USB controller support, I just redo the scripts, but instead Mouse X and Y, I do axis 3 and 4... It works, except now, the camera gets rotated by the controller, but the mouse is no longer able to rotate it. I think it has something to do with telling the script to assign the rotation to the camera from the controller right after the mouse, so it just chooses the one in front. THE ISSUE HAPPENS AROUND 95 Here is the script:
using UnityEngine;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(ConfigurableJoint))]
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3f;
[SerializeField]
private float thrusterForce = 1000f;
[SerializeField]
private float thrusterFuelBurnSpeed = 1f;
[SerializeField]
private float thrusterFuelRegenSpeed = 0.3f;
private float thrusterFuelAmount = 1f;
private float maxSpeed = 10f;
private float minSpeed = 5f;
public float GetThrusterFuelAmount()
{
return thrusterFuelAmount;
}
[SerializeField]
private LayerMask environmentMask;
[Header("Spring settings:")]
[SerializeField]
private float jointSpring = 20f;
[SerializeField]
private float jointMaxForce = 40f;
// Component caching
private PlayerMotor motor;
private ConfigurableJoint joint;
private Animator animator;
void Start()
{
motor = GetComponent<PlayerMotor>();
joint = GetComponent<ConfigurableJoint>();
animator = GetComponent<Animator>();
SetJointSettings(jointSpring);
speed = minSpeed;
}
void Update()
{
if (PauseMenu.IsOn)
{
if (Cursor.lockState != CursorLockMode.None)
Cursor.lockState = CursorLockMode.None;
motor.Move(Vector3.zero);
motor.Rotate(Vector3.zero);
motor.RotateCamera(0f);
return;
}
if (Cursor.lockState != CursorLockMode.Locked)
{
Cursor.lockState = CursorLockMode.Locked;
}
if (Input.GetButtonDown("Sprint"))
{
speed = maxSpeed;
} if (Input.GetButtonUp("Sprint"))
{
speed = minSpeed;
}
//Setting target position for spring
//This makes the physics act right when it comes to
//applying gravity when flying over objects
RaycastHit _hit;
if (Physics.Raycast(transform.position, Vector3.down, out _hit, 100f, environmentMask))
{
joint.targetPosition = new Vector3(0f, -_hit.point.y, 0f);
}
else
{
joint.targetPosition = new Vector3(0f, 0f, 0f);
}
//Calculate movement velocity as a 3D vector
float _xMov = Input.GetAxis("Horizontal");
float _zMov = Input.GetAxis("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
// Final movement vector
Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
// Animate movement
animator.SetFloat("ForwardVelocity", _zMov);
//Apply movement
motor.Move(_velocity);
//Calculate rotation as a 3D vector (turning around)
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
//Apply rotation
//Calculate camera rotation as a 3D vector (turning around)
float _xRot = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xRot * lookSensitivity;
// END OF MOUSE CONTROLS, NOW FOR CONTROLLER
float _ContyRot = Input.GetAxisRaw("Axis 3");
Vector3 _Controtation = new Vector3(0f, _ContyRot, 0f) * lookSensitivity;
//Calculate camera rotation as a 3D vector (turning around)
float _ContxRot = Input.GetAxisRaw("Axis 4");
float _cameraContRotationX = _ContxRot * lookSensitivity;
//Apply camera rotation
motor.RotateCamera(_cameraRotationX);
motor.Rotate(_rotation);
//Apply camera rotation from controller.
motor.RotateCamera(_cameraContRotationX);
motor.Rotate(_Controtation);
// Calculate the thrusterforce based on player input
Vector3 _thrusterForce = Vector3.zero;
if (Input.GetButton("Jump") && thrusterFuelAmount > 0f)
{
thrusterFuelAmount -= thrusterFuelBurnSpeed * Time.deltaTime;
if (thrusterFuelAmount >= 0.01f)
{
_thrusterForce = Vector3.up * thrusterForce;
SetJointSettings(0f);
}
}
else
{
thrusterFuelAmount += thrusterFuelRegenSpeed * Time.deltaTime;
SetJointSettings(jointSpring);
}
thrusterFuelAmount = Mathf.Clamp(thrusterFuelAmount, 0f, 1f);
// Apply the thruster force
motor.ApplyThruster(_thrusterForce);
}
private void SetJointSettings(float _jointSpring)
{
joint.yDrive = new JointDrive
{
positionSpring = _jointSpring,
maximumForce = jointMaxForce
};
}
}
Your answer
Follow this Question
Related Questions
How to rotate Z axis through Right Joystick (For 2D Topdown Game) 0 Answers
Trying to get camera to follow mouse, and player movement to be relative to camera angle? 0 Answers
How would do I play a walking animation when i move my joystick? 0 Answers
How to make my flashlight move with the mouse movement? 0 Answers