# 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);
}
}
``````

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

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