My C# gave me inverted controls for left and right WASD movement?
Okay so I followed this tutorial after deciding to have a go at making my own C# script for character movement: https://www.youtube.com/watch?v=miMCu5796KM
Which gave me an error(which is fixed now) and I ended up with this code:
using UnityEngine;
using System.Collections;
public class KalusBeta : MonoBehaviour { private CharacterController controller;
private float verticalVelocity;
private float gravity = 14.0f;
private float jumpForce = 10.0f;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
if (controller.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
Vector3 moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Vertical") * 5.0f;
moveVector.y = verticalVelocity;
moveVector.z = Input.GetAxis("Horizontal") * 5.0f;
controller.Move (moveVector * Time.deltaTime);
}
}
The previous script had Vertical for the moveVector.z axis and Horizontal for the moveVector.x axis. I had to switch the Vertical and Horizontal because pressing W or S made the player move left or right instead of forward or backwards while pressing A or D resulted in the player moving backwards or forwards.
The problem I'm having now is when I press A the player goes right instead of left and D makes the player go left instead of right. I don't know why this has been inverted or how to fix it, so I would seriously appreciate some help to point out where I've gone wrong and how I can fix it.
Below is a quick example of what is happening:
Again, any and all advice would be greatly appreciated :)
Answer by Caruzo · Mar 29, 2017 at 02:57 AM
moveVector.x = Input.GetAxis("Vertical") * -5.0f;
Try this one. I havent tested the code myself but if it does what you say, inverting the float value might have a positive effect.
That sort of worked :) After trying it and seeing it had a different effect, I then played around with + and - and this is now the working code:
Vector3 moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Vertical") * +5.0f;
moveVector.y = verticalVelocity;
moveVector.z = Input.GetAxis("Horizontal") * -5.0f;
controller.$$anonymous$$ove (moveVector * Time.deltaTime);
Thank you for showing me that :D