0
\$\begingroup\$

I have Unity 4.6.1 and I added a cube with a moving script and a rigidbody. The script is:

#pragma strict

var speed : float = 10.0;

function Update () {

    // Is the user pressing left or right (or "A" & "D") on the keyboard
    var horMovement = Input.GetAxis("Horizontal");
    // Is the user Pressing up or down (or "W" & "S") on te keyboard
    var forwardMovement = Input.GetAxis("Vertical");

    if (horMovement) {
        transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
    }

    if (forwardMovement) {
        transform.Translate(transform.forward * forwardMovement * Time.deltaTime * speed);
    }
}

And the rigidbody freezes position Y and freezes rotation X, Y, and Z. How do I stop it from bouncing back?

\$\endgroup\$
  • \$\begingroup\$ By bouncing you mean the slight movement backwards? \$\endgroup\$ – Vadim Tatarnikov Aug 5 '15 at 5:42
  • \$\begingroup\$ @VadimTatarnikov Yes I do mean that. \$\endgroup\$ – kprovost7314 Aug 5 '15 at 20:19
  • \$\begingroup\$ I've already wrote my answer \$\endgroup\$ – Vadim Tatarnikov Aug 5 '15 at 20:28
1
\$\begingroup\$

Use physics instead to move your object like rigidbody.AddForce(Vector3). Using physics will solve your problem with bouncing.

\$\endgroup\$
  • \$\begingroup\$ Where do I add that? \$\endgroup\$ – kprovost7314 Aug 5 '15 at 21:05
  • \$\begingroup\$ Instead of transform.Translate() \$\endgroup\$ – Vadim Tatarnikov Aug 6 '15 at 5:46
  • \$\begingroup\$ I replaced the whole line but it didn't work. \$\endgroup\$ – kprovost7314 Aug 7 '15 at 2:25
  • \$\begingroup\$ Your line should look like this GetComponent<Rigidbody> ().AddForce(YOUR VECTOR3 * SPEED); \$\endgroup\$ – Vadim Tatarnikov Aug 7 '15 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.