- Home /
Topdown player movement with controller problem
I'm having a problem with getting my player to move using the left analog stick with my controller. The player moves along the exact path of the analog stick, including moving back to the center.
I want the player controller to move the player freely without resetting back to the center. I've been trying to look at Unity's character controllers but have just been running around in circles.
Here's a .gif of what it looks like when I move the left analog stick around when testing: http://i.imgur.com/NBuq8D7.jpg
void Update ()
{
float h = Input.GetAxis ("Horizontal");
float xPos = h;
float v = Input.GetAxis ("Vertical");
float yPos = v;
transform.position = new Vector3 (xPos, yPos);
}
Answer by Cherno · May 19, 2015 at 08:41 PM
Use the Axis values to create a Vector3 and pass that to Transform.Translate, AddForce (rigidbody) or inputMoveDirection (CharacterMotor). As it is now, you just get the Axis value (-1 to 1), put that into another variable (h), and use that variable to set the transform.position... Which would, of course, always be between -1 and 1.
Here is a some code that does that, including Scaled Radial Deadzones.
public float moveAxisThreshold = 0.02f;
public Vector2 stickInput_l;
public Vector2 stickInput_r;
public float stickDeadzone = 0.02f;
private CharacterMotor cMotor;
public Vector3 moveVector;
void Start() {
cMotor = GetComponent<CharacterMotor>();
}
void Update () {
//Scaled Radial Deadzone
stickInput_l = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if(stickInput_l.magnitude < stickDeadzone) {
stickInput_l = Vector2.zero;
}
else {
stickInput_l = stickInput_l.normalized * ((stickInput_l.magnitude - stickDeadzone) / (1 - stickDeadzone));
}
moveVector = new Vector3(stickInput_l.x, 0, -stickInput_l.y);
cMotor.inputMoveDirection = moveVector;
}