Hey all,
i was just creating this FPS input controller but there are some problems.
basically i can walk up because that what i have told the script to do. If i press W he will go up. but if i press S he is meant to walk down but he walks up why is that ?
here is my script :
public var MoveSpeed: float = 4;
function Update () {
if(Input.GetAxisRaw("Vertical")){
var MoveFWD : float = Input.GetAxis("Vertical") * Time.deltaTime * MoveSpeed ;
transform.Translate(Vector3.forward * MoveSpeed);
}
// var MoveLeft : float = MoveSpeed * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
}
thank you so much in advance
You are moving with MoveSpeed instead of with MoveFWD, the value you’ve calculated. Additionally, you should use Mathf.Abs(GetAxis()) and compare it to some limit to avoid joystick calibration errors:
public var MoveSpeed: float = 4;
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.3){
var MoveFWD : float = Input.GetAxis("Vertical") * Time.deltaTime * MoveSpeed ;
transform.Translate(Vector3.forward * MoveFWD);
}
I fixed your formatting for you- it was all messed up.
Also- the problem doesn’t likely have anything to do with your code- chances are your input axis definitions are not set up properly.
Open your project in unity and go to edit->project settings->Input
and open up the two rollouts that say “Vertical”. The one at the top should be fore keyboard input. By default it uses up or W as the positive input, and S or Down as the negative input. I’m willing to bet that you either don’t have a negative button set, or it’s set to W as well. In case you were wondering, the second “Vertical” rollout is for joystick input.