- Home /
This question was
closed Aug 11, 2015 at 10:39 PM by
Razputin for the following reason:
Duplicate Question
Question by
Razputin · Aug 11, 2015 at 06:38 PM ·
inputcontrollercontrollersx-axis
Controller Support. Left Axis. How to code in C#?
I've programmed jumping and shooting into my game using Joystick button 0 and Joystick button 2. But I can't figure out how to make the player move left and right. Its a 2d game so all I need is the left analog stick to work for moving left and right. I know how to put it into unitys project inputs but not how to actually code it.
This is what my code looks like right now.
if(Input.GetKey("a") || Input.GetKey("left") ||Input.GetAxis("Xbox_Joystick_X-Axis")){
if(rigidbody.velocity.x > 0){
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x > -walkSpeed){
rigidbody.velocity.x -= 48*Time.deltaTime;
}
}
if(Input.GetKey("d")|| Input.GetKey("right") || Input.GetAxis("Xbox_Joystick_X-Axis")){
if(rigidbody.velocity.x < 0){
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x < walkSpeed){
rigidbody.velocity.x += 48*Time.deltaTime;
}
}
Obviously this is pretty wrong, I'm just really not sure how to do it.
Comment
Answer by tanoshimi · Aug 11, 2015 at 06:40 PM
Would I have to do something like
var h : float = Input.GetAxisRaw("Xbox_Joystick_X-Axis");
var xPos : float = h * range;
transform.position = new Vector3(xPos, 2f, 0);
if(Input.Get$$anonymous$$ey("a") || Input.Get$$anonymous$$ey("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.Get$$anonymous$$ey("d")|| Input.Get$$anonymous$$ey("right")||xPos > 0){
if(rigidbody.velocity.x < 0){
rigidbody.velocity.x = 0;
}
if(rigidbody.velocity.x < walkSpeed){
rigidbody.velocity.x += 48*Time.deltaTime;
}
}