1
\$\begingroup\$

Right now I'm using Rigidbody2d for the game character along with polygon collider 2D(2d platform game).

I'm beginner hope I'm using correct components.

enter image description here

And transform to move character from left to right.

Character.transform.Translate(Vector2.right * speed * Time.deltaTime);

And upward(jump).

Character.transform.Translate(Vector2.up * speed * Time.deltaTime);

When the game character moves on slopes there's lot of friction and bounciness and rotation and its worse when jumping from slopes.

\$\endgroup\$
  • \$\begingroup\$ The answer below about not wanting to use transform.translate is correct, but I suggest you modify the velocity of the RigidBody instead and pass Vector2.up/Vector2.down times the speed only. The implementation of RigidBody will handle the timestep \$\endgroup\$ – Athos vk Jan 21 '16 at 12:15
1
\$\begingroup\$

It is recommended to move through Rigidbody so that you can achieve natural behavior.

To do so, you can follow the following simple steps and write the following code in the script attached to your character.

// Class level variable
Rigidbody2D _rb;

void Start()
{
    // Assigning rigidbody2d component to variable
    _rb = GetComponent<Rigidbody2D>();
}

void Update()
{
    // Setting speed along x-axis
    _rb.velocity = new Vector2(speed,_rb.velocity.y);

    // For now it will jump on strike of space key. Please change it according to your input mehtod
    if (Input.GetKey(KeyCode.Space)){
        Jump();
    }
}

void Jump()
{
    // 300 is the jump force. Do change it accordingly.
    _rb.AddForce(0,300);
}
\$\endgroup\$
  • \$\begingroup\$ Should i use AddForce to move rigid body in x axis too? \$\endgroup\$ – user75648 Jan 21 '16 at 14:08
  • \$\begingroup\$ AddForce in Update? Don't even think about it :D \$\endgroup\$ – Hamza Hasan Jan 21 '16 at 14:09
  • \$\begingroup\$ And if you do AddForce in start then gradually your character will get slow down \$\endgroup\$ – Hamza Hasan Jan 21 '16 at 14:09
  • \$\begingroup\$ Then what should i use for smooth movement in x axis? \$\endgroup\$ – user75648 Jan 21 '16 at 14:09
  • \$\begingroup\$ I wrote _rb.velocity = new Vector2(speed,_rb.velocity.y); in update \$\endgroup\$ – Hamza Hasan Jan 21 '16 at 14:10
1
\$\begingroup\$

Dont use transform.Translate. it completely ignores collision. Try using rigidbody instead. It will make the movement smoother too

\$\endgroup\$
  • \$\begingroup\$ Can you give an example? \$\endgroup\$ – user75648 Jan 21 '16 at 12:19
  • \$\begingroup\$ After ataching rigidbody, do: Rigidbody2D rb = gameObject.getComponent(Rigidbody2D) and then rb.AddForce() \$\endgroup\$ – Bálint Jan 21 '16 at 12:26
  • \$\begingroup\$ AddForce makes the character rotate and move forward. \$\endgroup\$ – user75648 Jan 21 '16 at 12:40
  • \$\begingroup\$ rb.AddForce(transform.forward * 1000) also, you may neee to cap the speed using rigidbody.velocity.magnitude. There are plenty of tutorials online incuding the unity docs and the original tutorials, you should really look into that \$\endgroup\$ – Bálint Jan 21 '16 at 12:45

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.