I am trying to make an object that applies a little air blast to anything that gets above it. I am a little stuck here. The air blaster (aka Jet) should be able to find out if something is directly above it (relative to the jet's orientation) and, if so, apply a wind force that blasts the other object away. I have everything working except I can't make it work only if the other object is DIRECTLY above it.
I've tired using the dot product between the up Vector and the Vector between the object and the jet; I've tried a distance check; I've tried a combination of the two; I've even tried a Raycast. The Raycast would've worked only it would hit other objects (ones that I DON'T want to blast) and return Null Reference Exceptions.
Here's my code as it looks now:
public class JetController : MonoBehaviour
{
public int windForce = 0;
public GameObject[] balls;
// Use this for initialization
void Start ()
{
balls = GameObject.FindGameObjectsWithTag ("Ball");
}
// Update is called once per frame
void FixedUpdate ()
{
Vector3 wind;
balls = GameObject.FindGameObjectsWithTag ("Ball");
for (int i = 0; i < balls.Length; i++)
{
Vector3 heading = balls[i].transform.position - transform.position;
if (balls[i].transform.tag == "Ball")
{
if (Vector3.Distance(balls[i].transform.position, transform.position) < 2)
{
if (Vector3.Dot(heading, -transform.forward) > 0)
{
print (Vector3.Dot(heading, transform.up));
wind = transform.up * windForce;
balls[i].rigidbody.AddForce(wind);
}
}
}
}
}
If anyone could help, that would be much appreciated!
↧