Linecast and LayerMask

Hello everyone,
I am trying to use Linecast2D in my game but I didn’t able to get the result I want.

In my 2D strategy game Point Defense Turrets can intercept incoming enemy missiles, as you can see in the image above. When Linecast successfully hit the target missile, Turret should rotate toward the target missile. But it’s not happening. Because currently Force Field is blocking the Linecast.

This is my script attached on Point Defence Turret

void Start()
    {
        if(transform.parent.localPosition.y > 0)
        {
            defaultRotation = Quaternion.Euler(0, 0 , 90);
        }
        else if(transform.parent.localPosition.y < 0)
        {
            defaultRotation = Quaternion.Euler(0, 0 , -90);
        }

        transform.localRotation = defaultRotation;
    }
private int layerMask = 8;

    void FixedUpdate()
    {
        // Closest Missile is closestTarget
        if(closestTarget != null)
        {
            RaycastHit2D hit = Physics2D.Linecast(firePoint.position, closestTarget.position, layerMask);
            if(hit)
            {
                Debug.Log(hit.transform.name);
                // Something is blocking Linecast so rotate Turret to default State.
                transform.localRotation = Quaternion.Slerp(transform.localRotation, defaultRotation, Time.deltaTime * rotateSpeed);
            }
            else
            {
                // Linecast successfully hitting the target missile, so rotate toward the missile
                distanceFromMissile = Vector3.Distance (transform.position, closestTarget.position);
                if(distanceFromMissile < 40)
                {
                    Vector3 vectorToTarget = closestTarget.position - transform.position;
                    float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
                    Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
                    transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotateSpeed);
                }
            }
        }
    }

Let me know if you have any doubts.
Please help me, I am having trouble understanding Linecast and LayerMask from months.

Make sure you understand the difference between layers and layer masks:

Layers vs Masks:

2 Likes

Yes, I know this. Thanks for the reply.
What I am trying in above script is:
Linecast casting from the Point defense turret which is attached in the Player ships should only be blocked by the same Player ship only. How to achieve this? Player ship and its children Layer is Layer Number 3.

I think we can be pretty confident the Unity linecast API works as advertised, so it’s time to find out how you’re mis-using it.

First isolate the linecast in its own tiny scene and script and prove it is working properly with your layers.

Steps to success:

  • hard wire some positions
  • do the cast
  • check what comes back

Don’t do anything else until you prove you are using linecast and layermasks properly.

Once you prove that, then go back to your current logic and do the same thing all over, find where the issue is.

If the problem still persists, at least we know it’s not linecast.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like