0
\$\begingroup\$

i want my Player to be affected by gravity, while being able to be moved in horizontal direction(the Player jumps up and down), but my script:

Vector3 move = new Vector3(Input.GetAxis("Horizontal"), (Input.GetAxis("Vertical")), 0);

transform.position += move * speed * Time.deltaTime; 

...seems to nullifiy gravity. Can someone tell me how to fix that?

\$\endgroup\$
1
  • \$\begingroup\$ You should use Rigidbody.AddForce and steer very clear of altering the position by its transform. \$\endgroup\$
    – Savlon
    Mar 1, 2018 at 6:38

1 Answer 1

1
\$\begingroup\$

As Savlon wrote: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html or this: https://docs.unity3d.com/ScriptReference/Transform.Translate.html But do not use "transform.position +=" for player movement script.

\$\endgroup\$
1
  • \$\begingroup\$ Transform.Translate and transform.position += do very similar things in terms of interference with rigidbody physics, so I would not recommend using any Transform component positioning at all if you intend to move the body physically. \$\endgroup\$
    – DMGregory
    Mar 1, 2018 at 13:10

You must log in to answer this question.

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