- Home /
Trouble Understanding Vector3.Angle() Result
Hello everyone, I was looking at this post and tried to make my own little test script that could tell the angle between a projectile's velocity and the surface it hit.
The problem is, the angle returned is always either 90 or a result very close to 90 (89.xxx / 90.xxx), so I either set things up wrong, or I don't understand the result (and I should note that the upper plane is rotated 45 degrees, and the lower plane is rotated to 10 degrees).
Any clarification is appreciated.
using UnityEngine;
using System.Collections;
public class zTestProjectiles : MonoBehaviour {
//goes on the actual projectile prefab object,
//makes sure object "explodes" or bounces.
public float autoBounceAngle; //the angle at and over which a shell will always bounce
private Rigidbody thisRigidbody;
void Awake()
{
thisRigidbody = gameObject.GetComponent<Rigidbody>();
}
void OnCollisionEnter (Collision other)
{
Debug.Log (gameObject.name + " hit: " + other.gameObject.name);
Vector3 impactNormal = other.contacts[0].normal;
Debug.Log ("impactNormal is: " + impactNormal);
Debug.DrawRay(other.contacts[0].point, other.contacts[0].normal, Color.red, 2.0f);
Debug.Log ("thisRigidbody.velocity is: " + thisRigidbody.velocity);
CalculateImpactAngle(impactNormal);
}
//method to determine if the shell should explode or bounce on impact:
private void CalculateImpactAngle(Vector3 impactNormalAngle)
{
Debug.Log ("CalculateImpactAngle called");
float angle = Vector3.Angle (thisRigidbody.velocity, -impactNormalAngle);
Debug.Log ("angle is: " + angle);
}
}
Answer by SimonTS · Oct 22, 2014 at 05:40 PM
You should not use your rigidbody velocity for this. Instead use the relativeVelocity field of your Collision object, like this:
void OnCollisionEnter(Collision other) {
float angle = Vector3.Angle(other.relativeVelocity, other.contacts[0].normal));
// do stuff with angle
}
This will give you the angle between your projectile and the other colliders surface normal.
lg
Answer by popuppirate · Oct 22, 2014 at 12:03 PM
Try the following:
float angle = Vector3.Angle (thisRigidbody.transform.position.up, transform.somereference.up);
Make a reference object that is flat and non kinematic, like a plane, and use this as an up reference. The angle function will return the angle from the rigidbody's up to the reference's up in radians.
Convert radians to degrees with (180/(2*Mathf.PI))
This is off the top of my head, so sorry if it's not what you were after!
Cheers,
Popuppirate
This is off the top of my head, so sorry if it's not what you were after!
Not quite, but I still appreciate the effort.
@popuppirate: Your radians to degrees factor is wrong ^^
Either use
(180 / $$anonymous$$athf.PI)
or
(360 / (2*$$anonymous$$athf.PI))
But since Unity has this value as constant the easiest and safest way is to simply use
$$anonymous$$athf.Rad2Deg
Your answer
Follow this Question
Related Questions
RigidBody Script Conflict 1 Answer
C# Physics.LineCast Argument is Out Range 1 Answer
C# Check Physics.Raycast Once 0 Answers
C# Construct Line Links Over Time 0 Answers
Unexpected "Jumping" Behavior 0 Answers