Seeking Out a Target

Using Raycast to chase the player.

What is a Raycast and how do I use it?

According to Unity Manual Physics2D.Raycast

Casts a ray against Colliders in the Scene.

A raycast is conceptually like a laser beam that is fired from a point in space along a particular direction. Any object making contact with the beam can be detected and reported.

This function returns a RaycastHit object with a reference to the Collider that is hit by the ray (the Collider property of the result will be NULL if nothing was hit). The layerMask can be used to detect objects selectively only on certain layers (this allows you to apply the detection only to enemy characters, for example).

Overloads of this method that use contactFilter can filter the results by the options available in ContactFilter2D.

Raycasts are useful for determining lines of sight, targets hit by gunfire and for many other purposes in gameplay.

Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

See Also: LayerMask class, RaycastHit2D class, RaycastAll, Linecast, DefaultRaycastLayers, IgnoreRaycastLayer, raycastsHitTriggers.

The basic Physics2D.Raycast returns a Physics2D.RaycastHit2D and I need at least the the position to start the ray and direction the ray is going with the length.

This gives me the following results.

This is because the ray is hitting itself. There are several ways to fix this, using Physics Layers with a Layer Mask, giving the ray an offset that is outside the collider of the GameObject using it, or use one of the Raycast methods that gives you all of the colliders that it hit.

The difference between these to is one uses a list and the other uses an array. I will be using the array version. For the array size I will use 10 as I do not need to check against more objects then that. For the ContactFilter I want to make sure that useTriggers is true. I can then loop trough all of the Hits and use this data.

Now I filter out all of the Colliders that have the same Tag as this GameObject.

Now this works fine if I want something that is directly in front of the Enemy. If I need to check around the Enemy I will have to use one of the other types of Raycast.

On thing to note is when using the CapsuleCast is the difference between a Vertical and Horizontal CapsuleDirection.

CapsuleCast being used in game with a CapsuleCollider on the GameObject.

I am going to Use the BoxCast because it is easier to display a box in the Scene View. I need the contact filter to useTriggers.

I calculate the angle that the GameObject is rotated (this is going to be on the Z axis), this ensures that the BoxCast rotates with the game object.

The direction that the ray is cast in will cast in infinity in that direction I use Vector2.Zero to ensure that no matter where on the Z axis that the GameObject is the scene that the box will still hit something.

Doing Something Interesting

Up to this point I have been using the DrawGizmos method to just display the the collider now it is time to do something with the Physics2D.RaycastHit2D to make the Enemy move.

I get the first collider that has the tag that I want to follow as the target,

It is recommend to do any Physics related code in FixedUpdate. So this is where I set the target. I override the Enemy Movement Move method check to see if I have a target, if I do I rotate and move towards the target. If not I move normally.

I also changed the seek target to use a raycast now, this allows the Player to be able to possibly avoid the enemy homing missile.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Lafritz

Excited about changing my hobby into a new carer with GameDevHQ course.