- Home /
Question by
ticofa · Apr 27, 2015 at 12:50 AM ·
raycastvector3raycastingangleraycasthit
Problem With Raycasthit Angle
I'm trying to create an AI that "sees" an object, and depending on the raycast angle, when the distance is lower than what I specified, the AI turns left or right...
I kind of did the job on getting the angle at wich my raycasthit hits an object... the problem is, it is always a positive value, never negetive... for example
This is my script:
public Vector3 normal;
public RaycastHit hit;
public float _angleOfHit;
// Use this for initialization
void Start () {
}
void Raycaster(){
Physics.Raycast (this.transform.position, this.transform.forward * 200000, out hit);
Debug.DrawRay (this.transform.position, this.transform.forward * hit.distance, Color.red);
_angleOfHit = Vector3.Angle (hit.normal, -this.transform.forward);
}
// Update is called once per frame
void FixedUpdate () {
Raycaster ();
}
raycast-help.jpg
(124.9 kB)
Comment
When there is raycast hit you can get the transform angle and check for +45 or -45 degree rotation.