- Home /
AddForce on GetAxis always sam direction?
I am creating my own roll-a-ball script, and if the player touches the up, down, left, or right key the ball will roll in that direction. The problem is that if I press the left or right key, it will always go in the same direction. Same with the up and down keys. Here is my code:
var player : Transform;
function Start () {
}
function Update () {
if(Input.GetAxis("Horizontal")) {
player.rigidbody.AddForce (5, 0, 0);
}
if(Input.GetAxis("Vertical")) {
player.rigidbody.AddForce (0, 0, 5);
}
}
Does anyone know what I am doing wrong?
Answer by androids · May 17, 2015 at 01:01 AM
The reason why the player moves always the same way is because you command the player to move when you press the arrowkeys, no matter if you press the left or right arrow, it will move (5,0,0)
This will work. (NOT TESTED)
var player : Transform;
function Update ()
{
if(Input.GetAxis("Horizontal") > 0)
{
player.rigidbody.AddForce (5, 0, 0);
}
if(Input.GetAxis("Horizontal") < 0)
{
player.rigidbody.AddForce (-5, 0, 0);
}
if(Input.GetAxis("Vertical") > 0)
{
player.rigidbody.AddForce (0, 0, 5);
}
if(Input.GetAxis("Vertical") < 0)
{
player.rigidbody.AddForce (0, 0, -5);
}
}
Your answer
Follow this Question
Related Questions
limit AddRelativeForce 1 Answer
How to have vertical and horizontal axis work at the same time 1 Answer
Why is velocity checking intensive? 0 Answers
How to move a rigidbody along a curve or spiral 0 Answers
AddForce to position on Rigidbody 2 Answers