Ruben
Ruben

Reputation: 27

Obstacle Avoidance in Unity, using raycasts. C#

I am using some rudimentary obstacle avoidance algorithm to avoid obstacles, however i am having issues with the right and left sensors. When both are active. they will cancel their rotations and not rotate at all. Also having a bit of trouble figuring out hit.normals via the front sensor.

    ray = new Ray(transform.position + Vector3.up, transform.forward);
    Vector3 posInicial = transform.position;

    if (Physics.Raycast(ray, out hit, 55f)) // Front sensor
    {
        if (hit.collider.tag == ("Pick Up")) // If robot detects pick up, it goes towards it
        {
            Debug.DrawLine(ray.origin, hit.point, Color.red);
            transform.position = Vector3.MoveTowards(transform.position, hit.point, Time.deltaTime * speed);
        }
        else
        {
            transform.Rotate(0, -80 * Time.deltaTime, 0); // Rotate if front sensor doesn't detect pick up
            Debug.DrawLine(ray.origin, hit.point, Color.blue);
        }
    }
    else
    {
        transform.position += transform.forward * speed * Time.deltaTime; // Go forward
        Debug.DrawLine(ray.origin, hit.point, Color.white);
    }
    if (Physics.Raycast(posInicial, Quaternion.AngleAxis(45f, transform.up) * transform.forward, out hit2, 20f))
    {
        transform.Rotate(0, -80 * Time.deltaTime, 0); // Rotate left if right detected
        Debug.DrawLine(posInicial, hit2.point, Color.yellow);
    }
    if (Physics.Raycast(posInicial, Quaternion.AngleAxis(-45f, transform.up) * transform.forward, out hit3, 20f))
    {
        transform.Rotate(0, 80 * Time.deltaTime, 0); // rotate right if detected left
        Debug.DrawLine(posInicial, hit3.point, Color.cyan);
    }
}

Upvotes: 0

Views: 4051

Answers (1)

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

I am not sure why you are trying to implement this on your own. If it is because you are interested in the algorithm and you are trying to extrapolate it in the future to the real world, it's fine.

But just in case, there is already something called NavMesh in Unity, which basically provide a AI to gameobejcts, for example enemies, which make them identify obstacles so to avoid them when moving from one point to another in the scene.

  • Select scene geometry that should affect the navigation – walkable surfaces and obstacles.
  • Check Navigation Static on to include selected objects in the NavMesh baking process.
  • Adjust the bake settings to match your agent size.
  • Click bake to build the NavMesh

Once you have the scene baked, you can add the navmesh to a gameobject:

public class EnemyMovement : MonoBehaviour
{
    NavMeshAgent nav; 

    function Awake ()
    {
        nav = GetComponent(NavMeshAgent);
    }

    //So here it will be chasing the player
    function Update ()
    {
        nav.SetDestination (player.position);
    }
}

You can read more about this here and follow this tutorial

Upvotes: 2

Related Questions