Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Movement with AddForce: Wrong Direction

$
0
0
Hey everyone; I have a Player Controller script that is supposed to apply a force to the player GameObject to make it move in a direction based upon user input. public void MoveUpdate () { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); if(h * this.rigidbody.velocity.x < maxSpeed) { this.rigidbody.AddForce(Vector3.right * h * moveForce); } if(Mathf.Abs(this.rigidbody.velocity.x) > maxSpeed) { this.rigidbody.velocity = new Vector3(Mathf.Sign(this.rigidbody.velocity.x) * maxSpeed, this.rigidbody.velocity.y, this.rigidbody.velocity.z); } if(v * this.rigidbody.velocity.z < maxSpeed) { this.rigidbody.AddForce(Vector3.forward * v * moveForce); } if(Mathf.Abs(this.rigidbody.velocity.z) > maxSpeed) { this.rigidbody.velocity = new Vector3(this.rigidbody.velocity.x, this.rigidbody.velocity.y, Mathf.Sign(this.rigidbody.velocity.z) * maxSpeed); } if(h > 0 && !facingRight) { Flip(); } else if(h < 0 && facingRight) { Flip(); } } When I press "w" or "up," the forward input buttons (a.k.a. Input.GetAxis("Vertical")), the player also moves a little bit to the left or right, though. Why might this be happening? I would like the player to only move forward if only the "w" or "up" inputs are activated. Edit: I've verified that the "h" value is never anything higher or lower than zero when this happens, and I've tested this script on a basic cube object, with the same results.

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>