Monday, February 22, 2016

unity - Need help with a field of view-like collision detector!


I ran into a trouble while making a field of view for my character. I figured how to make it work with a Linecast, but what I really need is a cone-shaped field, so that the character can detect objects or enemies if they get in that field of vision. It also needs to be intersectable by other objects, so OnCollisionEnter won't work. I suspect that Raycasting might solve the problem, but I couldn't quite understand it's workings, because I'm still new. I would really appreciate any ideas that might help to solve it. Here is what I need:


collision detector


Here is what I have so far, but this detector is unsuitable, because it can only detect objects in a straight line:


public Transform sightStart, sightEnd;
// Update is called once per frame
public bool objectSpotted = false;
void Update () {
Raycasting1 ();
}

void Raycasting1()
{
Debug.DrawLine(sightStart.position, sightEnd.position, Color.green);
objectSpotted = Physics2D.Linecast (sightStart.position, sightEnd.position);
}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...