Tom
Tom

Reputation: 13

Unity 2D, C# 2D Raycast giving strange distance results

So I'm trying to use a 2D raycast to stop the laser sight in my game going through walls.

First I tried simply setting the end point of the laser to the hit point. Yet that provided strange results, with the hit point seemingly being in a random place, normally behind the player.

I did a debug to check that the raycast was facing the way I wanted (though I know the debug uses to 3D raycast system, so maybe I was doing something wrong there?)

I then tried using the distance of the hit point. It's working, sort of, when the laser hits a wall, it changes it's length so it doesn't hit the wall. The problem is that it stops long before the wall, and not at a specific point either, if I walk closer to the wall the line will change almost as if it's trying to keep a division of the distance away from the wall. It's probably something really simple but I can't for the lift of me work it out.

The laser sight is deep down in a hierarchy of the players components, some of which have scale, I'm wondering if this might be the reason for the odd division, and if so, how to correct it.

Thanks in advance for any help!

using UnityEngine;
using System.Collections;

public class LaserSight : MonoBehaviour {

    private LineRenderer Lren;
    public void Start () {
        Lren = GetComponent<LineRenderer>();
    }

    public void Update() {
        if (Lren.enabled) {
            int layerMask = (1 << LayerMask.NameToLayer("Wall")) | 
                            (1 << LayerMask.NameToLayer("Enemies"));
            //layerMask = ~layerMask;
            RaycastHit2D hit = Physics2D.Raycast(transform.position,
                                                 transform.right, 
                                                 layerMask);
            if (hit) {
                Lren.SetPosition(1,-hit.point); 
                //Lren.SetPosition(1, new Vector3(hit.distance, 0, 0));
            } else {
                Lren.SetPosition(1, new Vector3(100, 0, 0));
            }
        }
    }
}

Upvotes: 0

Views: 959

Answers (1)

Tom
Tom

Reputation: 13

I think I fixed it, the problem was to do with converting world space to local space. This is the code I ended up writing

using UnityEngine;
using System.Collections;

public class LaserSight : MonoBehaviour {   
   private LineRenderer Lren;

   public void Start () {
       Lren = GetComponent<LineRenderer>();
   }

    public void Update() {
        if (Lren.enabled) {
            int layerMask = (1 << LayerMask.NameToLayer("Wall")) | (1 << LayerMask.NameToLayer("Enemies"));
            RaycastHit2D hit = Physics2D.Raycast( transform.position, transform.right , 100, layerMask);
            if (hit) {
                Lren.SetPosition(1, new Vector3 (transform.InverseTransformPoint(hit.transform.position).x, 0 , 0));
                //Lren.SetPosition(1, new Vector3( hit.distance, 0 , 0));
            } else {
                Lren.SetPosition(1, new Vector3( 100, 0 , 0));
            }
        }

    }
}

Upvotes: 1

Related Questions