- Home /
jumping at a degrees angle
I have this simple script which makes the character but I don't know how to make a the character move at an angle e.g. 45 degrees. I have already tried it but it is not working. I have tried changing the values but it doesn't work the way I want. It is meant to move upwards at a 45 degree angle but it doesn't.
rigidbody2D.AddForce (new Vector2 (0f, jumpForce));
The code above is the original but I changed it to this and still doesn't work.
rigidbody2D.AddForce (new Vector2 (5f, jumpForce));
It still doesn't work so how do I do it. I'm working in C#. Thanks in advance for any replies. Oh and I have already tried searching but nothing came up on this.
Answer by Eno-Khaon · Apr 29, 2016 at 09:32 AM
If you want your character to always jump at a 45 degree angle (from flat ground, at least), regardless of their current speed, you should be able to do it with something like:
rigidbody2D.velocity += (Vector2.up * rigidbody2D.velocity.x);
Modifying the velocity directly rather than using AddForce, in this case, will ensure that mass is not a factor in the force added.
Will this work for Y axis because it is and endless 2D game in the Y axis. and would I change: rigidbody2D.velocity.x to this: -rigidbody2D.velocity.x for the other direction e.g. one for the right and one for the left. Thanks
Ah, right. $$anonymous$$y mistake on that one. You can just go with:
$$anonymous$$athf.Abs(rigidbody2D.velocity.x)
and it'll always be a positive value.
rigidbody2D.velocity += (Vector2.up * $$anonymous$$athf.Abs(rigidbody2D.velocity.x));
(Argh! Why do I keep writing things when I'm tired?)
Thanks for your help but it doesn't work. I will try to fix it my self again
Your answer
Follow this Question
Related Questions
How can I make an object stop all momentum and hold it's position in air? 1 Answer
2d Raycasting, trouble drawing rays at the correct angles 1 Answer
How to calculate angle between 2D objects and use the result? 0 Answers
Math to have a bullet penetrate stuff in 2D? 1 Answer
How can I limit the rotational speed of a gameObject tracking my mouse? 1 Answer