- Home /
Question by
Nishimaster · Jul 20, 2016 at 09:02 PM ·
rotationraycastingraycasthit2d
Issue with rotating raycast.
I am trying to make a laser which rotates in a circle and draws a line upon collision with things. However, the raycast seems to be lagging behind the aim position in the code when it collides with something.
Using debug, blue is from the origin position to the raycast hit point, and red is the aim position which the raycast is aiming at. See the gif below (sorry for low framerate)
http://puu.sh/q7b2q/a29e6d5cde.gif
Below is the code which i'm using. Any assistance is appreciated!
public class RotatingLaser : MonoBehaviour {
private Transform center;
public float degreesPerSecond = -65.0f;
private Vector3 v;
public Vector3 aimPosition;
private LineRenderer line;
public RaycastHit2D hit;
// Use this for initialization
void Start()
{
line = GetComponent<LineRenderer>();
line.SetColors(Color.red, Color.red);
line.SetWidth(0.1f, 0.1f);
center = transform;
aimPosition = new Vector2(0.1f, 0.1f);
}
void Update()
{
}
// Update is called once per frame
void FixedUpdate()
{
//move the aim position in a circle
v = aimPosition - center.position;
v = Quaternion.AngleAxis(degreesPerSecond * Time.deltaTime, Vector3.forward) * v;
aimPosition = center.position + v;
//debug the aim position
Debug.DrawLine(transform.position, aimPosition, Color.red);
hit = Physics2D.Raycast(transform.position, aimPosition, 5f);
//line.SetPosition(0, transform.position);
if (hit.collider != null)
{
Debug.DrawLine(transform.position, hit.point, Color.blue);
//line.SetPosition(1, hit.point);
}
else
{
//line.SetPosition(1, aimPosition);
}
}
}
Comment