Question by
MTom · Mar 07, 2016 at 08:30 PM ·
unity 2dlinerendererraycasthit2d
2D Raycast not colliding, but only some of the time...
So you can see my problem here:
https://www.youtube.com/watch?v=5K-0ES8EG4g
the laser sights will occasional clip clean through the walls, or stop short randomly. Other times they work as intended. I can't really work out what's causing this.
Here is my code.
using UnityEngine;
using System.Collections;
public class LaserSight : MonoBehaviour {
private LineRenderer Lren;
public void Start () {
Lren = GetComponent<LineRenderer>();
}
public void FixedUpdate() {
if (Lren.enabled) {
int layerMask = (1 << LayerMask.NameToLayer("Wall")) | (1 << LayerMask.NameToLayer("Enemies"));
RaycastHit2D hit = Physics2D.Raycast( transform.position, transform.right , 100, layerMask);
Debug.DrawRay (transform.position, transform.right);
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));
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I make a laser that bounces around a room? 0 Answers
Unity 2D: how to fire a renderline with raycast upward? 0 Answers
RayCast 2d change start position ? 0 Answers
Raycast 2d is not working at all 0 Answers