# 2D Linecast does not work

**URL:** <https://discussions.unity.com/t/2d-linecast-does-not-work/591803>\
**Category:** Physics\
**Tags:** Physics\
**Created:** [July 31, 2015, 10:47pm UTC](https://discussions.unity.com/t/2d-linecast-does-not-work/591803 "2015-07-31T22:47:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SymphonyX](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@SymphonyX](https://discussions.unity.com/u/SymphonyX)\
**Post date:** [July 31, 2015, 10:47pm UTC](https://discussions.unity.com/t/2d-linecast-does-not-work/591803/1 "2015-07-31T22:47:40Z")

</div>

Hello,  
I have wrote a little enemy movement script (only idle movement so far), and I have trouble keeping my enemy behind the walls. I have a 2D game, so the camera is orthographic from above.  
The enemy has a script attached that does the following:

```csharp
void Update ()
   {
     if (target == null) {
       if (frameSkip == 5 && movementCalculating == false) {
         IdleMovement ();
         frameSkip = 0;
       }
     } else {
       ChasePlayer ();
     }
     frameSkip++;
   }

```

target is set when the player enters the enemie’s circlecollider. Frameskip so movement will be smoother.

Then the IdleMovement() function:

```csharp
void IdleMovement ()
   {
     movementCalculating = true;
     var randomDirection = movementVectors [Random.Range (0, 4)];
     var wallAhead = AnyWalls (randomDirection);
     if (!wallAhead) {
       PlayMoveAnimation (randomDirection);
       transform.Translate (randomDirection * Time.deltaTime * movementSpeed);
     }
     movementCalculating = false;
   }

```

The movementVectors are defined as follow:

```csharp
movementVectors = new Vector2[4] {
       Vector2.left,
       Vector2.up,
       Vector2.right,
       Vector2.down
     };

```

The AnyWalls() function is my 2D Linecast:

```csharp
bool AnyWalls (Vector2 dir)
   {
     Vector2 start = transform.position;
     Vector2 end = start + dir;
     myCollider.enabled = false;
     var hit = Physics2D.Linecast (start, end, rayCastLayerMask);
     myCollider.enabled = true;
     if (hit.transform != null) {
       return true;
     } else {
       return false;
     }
   }

```

The rayCastLayerMask is set to the blocking layer, the same layer the walls are on.  
The problem is that the enemy is walking through walls as if there were none.  
I have no idea why this isn’t working.

Any help would be greatly appreciated!  
Thanks,  
Sym

---

<div class="post-metadata">

**Author:** ![Rokkimies](https://avatars.discourse-cdn.com/v4/letter/r/22d042/32.png) [@Rokkimies](https://discussions.unity.com/u/Rokkimies)\
**Post date:** [August 1, 2015, 1:41am UTC](https://discussions.unity.com/t/2d-linecast-does-not-work/591803/2 "2015-08-01T01:41:18Z")

</div>

For the character movement I I’ve read Vector2.left and up have been deprecated so that might cause some issues.

You set linecast end to start + dir; but whats exactly in dir? Have you checked Debug.Log() or print(([hit](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=hit).transform) or print(hit.Name) at spots where you’re sure the object is facing a wall and see if it returns anything. I’ve only done with Raycast, but you might also want to try to see what’s in your hit variable after raycast onto wall. Because I suspect

```csharp
  var hit = Physics2D.Linecast (start, end, rayCastLayerMask);

```

is actually returning true or false, which would affect your later if-loop so that it always returns false.
