1
\$\begingroup\$

I want to make a little game in Unity, where the player (a ball) jumps up and down and you have to maneuver it through (flappy bird like) obstacles.

My problem is, after I added a simple movement script:

float speed 10f;
Vector3 move = new Vector3(Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0);
transform.position = move * speed * time.deltatime;

The ball is stuck and can only be moved a little,before returning to its start position.

My question is how i can fix that? I didnt find any solution

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

The problem is in this line:

transform.position = move * speed * time.deltatime;

If the value of move*speed*Time.DeltaTime is lets say, (0,3), in that line of code you are not telling your transform to move (0,3) distance, but to set its absolute position to (0,3)

You should be updating the position of the player relative to it's current position:

transform.position = transform.position + move*speed*Time.DeltaTime

Or if you prefer a more compact line of code:

transform.position += move*speed*Time.DeltaTime
\$\endgroup\$
1
  • \$\begingroup\$ Mark the answer as valid, so it's listed as answered please. \$\endgroup\$
    – Leo
    Commented Feb 28, 2018 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .