- Home /
C# How to program X-Axis on Controller.
This is what I have right now, but I'm really not sure what it should actually look like, I can't find example code or anything like that anywhere.
var h : float = Input.GetAxisRaw("Xbox_Joystick_X-Axis");
var xPos : float = h * range;
transform.position = new Vector3(xPos, 2f, 0);
if(Input.GetKey("a") || Input.GetKey("left")||xPos < 0){
if(rigidbody.velocity.x > 0){
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x > -walkSpeed){
rigidbody.velocity.x -= 48*Time.deltaTime;
}
}
//if the player pressed D, add velocity to move right.
if(Input.GetKey("d")|| Input.GetKey("right")||xPos > 0){
if(rigidbody.velocity.x < 0){
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x < walkSpeed){
rigidbody.velocity.x += 48*Time.deltaTime;
}
}
http://docs.unity3d.com/ScriptReference/Input.GetAxis.html
Unitys Reference page gives a pretty clear comment on how to do this. There is even a whole script provided by them. And there are actually tons of movement scripts on the web.
I'm aware of the doc that's where I got the first part from. So that doesn't really help me.
I know that. I'm asking what is wrong with my code. Is xpos not representing whatever the float is between -1 and 1
I assume this script is in the function Update or FixedUpdate. + I dont know what walkSpeed means because there is no var that refers to it in your example.
Update. And I am aware this script is shite. It was one of the earliest ones I did that's why I really need help fixing it lol.
Your answer
Follow this Question
Related Questions
Unity ignoring joysticks of specified number 0 Answers
Unity Controller Support Help 0 Answers
Help Needed: How to Use Input Manager on UI Button 0 Answers
How do I standardize my game's input to different types of controllers? 1 Answer
How do you properly deal with analog joystick input on multiple controller inputs? 1 Answer