Question by
MadeByHumans · Aug 26, 2021 at 04:40 PM ·
raycast2d gametop down shooter
Top Down Raycast
Hi, me and my friend want to do a little top-down 2D game and are currently stuck at Raycast. We would like to add a dash ability, but you need a raycast to check whether there is an object in front of the player or not. I wrote a few lines of code and tested the game, and I noticed that the Raycast behaved very strangely and erratically. It would be nice if someone could help us to produce a raycast that is always done in front of the player.
My code looks like this:
// Update is called once per frame
void Update()
{
Ray2D ray = new Ray2D();
//beginning of the ray
ray.origin = transform.position;
//direction of the ray
ray.direction = transform.up;
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, shootingRange, 1 << 8);
Debug.DrawLine(ray.origin, ray.direction * shootingRange, Color.red);
if (hit.collider != null)
Debug.Log(hit.collider.tag);
}
}
(I also added layers but I doesnt seem to work)
Comment