Question by
lukas0499 · Jun 22, 2018 at 05:24 PM ·
2draycastraycastingrayraycasthit2d
2D raycast not working
Hello everyone!
So I have this piece of code:
void MoveUp ()
{
Vector3 direction = Vector3.up;
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up, distance, 8);
if (hit)
{
if (hit.transform.tag == "Wall")
{
WallInFront();
}
else if (hit.transform.tag == "Enemy")
{
Attack();
}
}
else
{
if(move == false)
{
// set position to move
desiredPosition += direction;
move = true;
}
}
}
basically this piece of code gets executed when I swipe (this code is for swipe up, there's code for down left and right that are the same except for "Vector2/3.up"
what I want is to raycast and see what the raycast hitted, if it was a wall, do soemthing, if theres nothing in front, move
I cant seem to make this work.
Comment