- Home /
Return Angle as Vector 3
Hello, I have to raycast into a direction to check if there is a clear path between two objects. To do that I need a vector 3 of an angle. How would I do this.
Thanks,
Thor
I'm not sure what you are asking. You don't need an angle to get a vector3. If you are casting from object1 to object2, you calculate the direction:
var direction = object2.transform.position - object1.transform.position;
Note you might consider a Physics.Linecast() ins$$anonymous$$d of a Raycast().
Not sure why u want an angle because the question is unclear. Going off this though, I'll give an example of say, you want to see if there is something in front of a box. Then print how far away this object is from your box.
//C# Example
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour
{
RaycastHit hit;
void Update()
{
if(Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity))
{
float distance = Vector3.Distance(transform.position, hit.collider.transform.position);
print("Distance from " + hit.collider.gameObject.name + " is " + distance + " units. ");
}
}
}
Answer by ebogguess · Jul 02, 2013 at 05:29 AM
The vector between two objects is the difference between their transform position vectors. If you can ray cast in that direction, from one of the objects and hit the other, then you have line of sight.
Answer by Owen-Reynolds · Jul 02, 2013 at 02:08 PM
If you have angle A, which will be a Quaternion, then a vector pointing that way is A*Vector3.forward;
If you have an angle in mind, like 30 degrees on y, 5 tilted up on x, then Quaternion.Euler(-5,30,0)*Vector3.forward;
gives a length 1 V3 aimed that way (negative x is up -- left-handed rule.)
Most people would probably just guess that Vector3(1,0.1f,0.8f)
was at about that angle, so is good enough.
transform.forward
is shorthand for transform.rotation*Vector3.forward;
That math is: Unity thinks 0 degrees on x and y is North. So, Quaternion.identity*Vector3.forward
gives back Vector3.forward
, unchanged, which is North, which is rotation 00.
As the other posters say, you usually got the angle starting with an aim point or a normal or something like that. So it's usually easier to just use the starting point directly and skip ever having the angle.