- Home /
Jump angle relative to surface?
Hey men and gentleladies! I'm rather new to Unity, but I'm learning fast. I'm trying to make a jump script that makes the rigidbody 'player' jump in an angle relative to the surface it's placed on. I.E. if you're standing on a 45degree angled surface, the player doesnt jump straight up, but jumps out in a 45degree angle, 90degrees on the surface. I'm using C# for all my scripts, but I have no clue on how to make this. There's no constant 'up' direction of my player, since its a rolling sphere, so I can't use that, unfortunately. Anyone have an idea of how I can solve this in a good way? :) Thanks in advance
Answer by Berenger · Jan 28, 2013 at 11:11 AM
Hi !
So, you need the normal of the ground you're rolling on. As you're using a rigidbody, you can use the function OnCollisionStay, which gives you access to the contact points and the normal.
I'm not explaining everything, partly to keep a bit of mistery, but mostly because I think people learn better when they have to search a bit, once on the right path.
Good luck :)
Hey Berenger, thank you very much for the answer, this surely got me on the right track :) I appreciate you not giving it all away, as I prefer to learn as I go!
I'm trying to implement the same feature through JavaScript. I've got a rolling ball that I'd like to make push off whatever surface it's touching when the "Jump" button is hit, rather than just moving straight upwards. I'm a novice at this, and scripting isn't my forté; I could use some more hand-holding on this.
#pragma strict
var jumpHeight = 8;
var distToGround : float;
// Adds upwards velocity to the ball when "Jump" is hit
function Update (){
if (Input.GetButtonDown("Jump") && IsGrounded ()){
rigidbody.velocity.y = jumpHeight;
}
}
// Checks whether or not the ball is on the ground
// This only works vertically — steep slopes won't trigger it
function IsGrounded () : boolean{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
That's what I've got thus far. It only makes the ball move up, and won't even work if the slope is too steep. I'm not sure how to improve upon it. While hints are good for some people, I'm still stumped; I don't know coding very well at all, but have been learning a lot from examples. If someone could show how I could use OnCollisionStay to get the normals and make this work, I'd very much appreciate it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091728im_/https://answers.unity.com/themes/thub/images/avi.jpg)