0

I wish to create a ball character similar to red ball game in unity 2d but I can't seem to get it to work like the one in red ball. I wish for the ball to ROLL left and right and be able to Jump. I managed to make it roll left and right by adding a physics material and bumping up the friction and adding the rb.AddForce() function but I am having trouble with the jumping. I tried rb.velocity() but when I jump and move right or left, the ball adds force too strongly and it just moves too swiftly . Am I missing something or is there a better way of doing this? I need help.....

Jibril
  • 17
  • 1
  • 1
  • 5

1 Answers1

0

A quick fix for this would be to define an upper limit. Like:

float limit = 10f;
Rigidbody2D rig;

void Start(){
  rig = gameObject.transform.GetComponent<Rigidbody2D>();
}

void Update(){
if(Input.GetKeyDown(KeyCode.A && rig.velocity.magnitude < limit){

  rig.AddForce(accelerationVariable);
 }
}

I would use Rigidbody.velocity.magnitude because it gives you the length of the vector. If you just want to check the x-Force use Rigidbody.velocity.x

Hope that helps

  • I think it's better to use the AddForce inside the `FixedUpdate`, since you need the same time step for [keeping the physics consistent all time](https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html). – neobits Apr 12 '22 at 10:06