0
votes

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.....

1
Possible duplicate of Unity 2d jumping script - Ignacio Alorre

1 Answers

0
votes

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