Question by
cluin14 · Jun 13, 2020 at 06:36 PM ·
movementcharacter controllerstrafing
Third person mouse rotation with strafe C#
Hello, thanks for taking the time to read over this. I'm currently trying to have my character strafe using the AD keys, and use the mouse for rotation. Overall, movement is working great but I cannot blend strafing with the rest of the movement. What I mean is if I'm standing still then my character can AD strafe, but if I'm moving forward or backwards and turning with mouse, my AD keys will no longer cause my character to strafe. It's either strafe or normal movement.
I feel like my transforms my be conflicting but I'm not 100% sure on how to solve this.
private CharacterController characterController;
[SerializeField]
private float forwardMoveSpeed = 5.5f;
[SerializeField]
private float backwardMoveSpeed = 3;
[SerializeField]
private float turnSpeed = 155f;
[SerializeField]
private float verticalVelocity;
[SerializeField]
private float strafeSpeed = 3f;
private bool mouseLocked;
private void Awake()
{
characterController = GetComponent<CharacterController>();
}
private void Update()
{
//movement controls
var horizontal = Input.GetAxis("Mouse X");
var vertical = Input.GetAxis("Vertical");
var strafe = Input.GetAxis("Horizontal");
var movement = new Vector3(horizontal, 0, vertical);
transform.Rotate(Vector3.up, horizontal * turnSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * strafeSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * strafeSpeed * Time.deltaTime);
}
if (vertical != 0)
{
float moveSpeedToUse = (vertical > 0) ? forwardMoveSpeed : backwardMoveSpeed;
characterController.SimpleMove(transform.forward * moveSpeedToUse * vertical);
}
Any help is much appreciated and again, thank you for taking the time to read over this.
Comment