- Home /
Determining the angle of a projectile collision.
I am trying to make a projectile that when colliedes with a surface at a 45 degree or lower will bounce and keep moving. I was trying to implement that with this code, but sadly I can't get it to work.`if (bounce){
var angle = Vector3.Angle(collision.contacts[0].normal, transform.forward);
Debug.Log(angle);
if (angle < bounceAngle){
transform.position = collision.contacts[0].point + Vector3(0, 1, 0);
}else{
bounce = false;
}`
EDIT I don't see where you set bounce to true. I see where you set it to false.
it's before this code sorry. I'm sure the problem is in the angle some were but i can;'t figure it out
shouldn't it be if(angle>bounceAngle){
, and why are you adding 1 in the positive y-direction shouldn't you be reflecting it across the plane perpendicular to the collision?
Hi, I believe, we look for the same solution. Here I added two pictures, to better representation. As far as I understood here, also no solution was found here. Please help us community.
Answer by voodoo · Dec 30, 2012 at 07:42 PM
Off the top of my head, give this a try:
// Reflect the trajectory of the contact
var reflected : Vector3 = Vector3.Reflect( transform.forward, collision.contacts[0].normal );
// Compare how alike the reflection and the original direction are
if ( Vector3.Dot(reflected, transform.forward) > 0.5 ) {
// Do bounce logic
} else {
// Do not-bounce logic
}
See: Vector3.Dot, Vector3.Reflect
Thanks this worked :D! But now i need to make the projectile move perpendicular to the surface it hit but keep it's trajectory.
So you want it to slide along the surface it hits, or bounce off in the same direction? $$anonymous$$eep in $$anonymous$$d we already have our 'reflect' variable that gives you the direction of reflection. ;)
When I assign reflected to the transform.rotation it always starts moving in the global z axis only. How do I fox that?
Try using: transform.LookAt( transform.position + reflected );
I think that should do it.
Answer by Drakmyth · Dec 30, 2012 at 08:30 PM
It's a little hard to tell without more code, but the first thing I notice is that your first line there is measuring the angle between the forward vector of the projectile (assuming this script is attached to the projectile) and the normal of the collision. If I'm not mistaken, the collision normal is perpendicular to the surface, which means the angle you want is (90 - angle), not angle itself.
with the boolean check given on line 3 yes the measure should be the angle between the normal, and the forward, but if the OP reverses the boolean check then the angle measure can be maintained.
what gets me is what is going on with the + Vector3(0,1,0). what does moving it 1 unit along the positive y-axis have to do with reflection.
Your answer
Follow this Question
Related Questions
Bounce into the air 1 Answer
Float to angle coordinate? 1 Answer
Shooting Script Bullet Doesnt Fire 2 Answers
Calculating a projectiles angle required to hit object. 2 Answers