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.....
Asked
Active
Viewed 621 times
0
-
Possible duplicate of [Unity 2d jumping script](https://stackoverflow.com/questions/25350411/unity-2d-jumping-script) – Ignacio Alorre May 16 '19 at 06:43
1 Answers
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

Franz Wachter
- 81
- 3
-
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