Question by
zachmallet · Oct 24, 2016 at 01:07 AM ·
raycastpositionmousepositionraycasthit2d
Inconsistent raycast update
I'm currently using raycast to have my player teleport around. Its very inconsistent and very bugy on what it detects. Heres my code: Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
posy = Mathf.Clamp(mousePos.y, player.position.y - tpRange, player.position.y + tpRange);
posx = Mathf.Clamp(mousePos.x, player.position.x - tpRange, player.position.x + tpRange);
newPos = new Vector2(posx, posy);
distance = Vector2.Distance(this.transform.position, newPos);
RaycastHit2D hit = Physics2D.Raycast(player.position, mousePos, tpRange);
Debug.DrawLine(this.transform.position, newPos, Color.red);
Debug.DrawLine(hit.point, this.transform.position, Color.green);
if (hit)
{
if (hit.collider.tag == "Wall")
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
this.transform.position = hit.point;
Player.myPlayer.currentPlayerHealth -= 5;
}
}
else
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
this.transform.position = newPos;
Player.myPlayer.currentPlayerHealth -= 5;
}
}
}
else
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
this.transform.position = newPos;
Player.myPlayer.currentPlayerHealth -= 5;
}
}
Heres a video of the problem im having: https://gfycat.com/WickedOptimisticHorse
I should be having a raycast be launched every update frame and have it constantly updating. But it seems like like keeping its same hit.point even if my mouse position moves.
Comment