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