S
S
Sergey Ivanov2019-03-26 15:16:24
C++ / C#
Sergey Ivanov, 2019-03-26 15:16:24

Unity raycast hit 2d how to track void?

I shoot from raycast it hits but I can't track when it shoots into the void it just doesn't work

RaycastHit2D hit = Physics2D.Raycast(Vector2.down, Vector2.up);
if (hit.collider == null)
{
scrollSpeed = Mathf.MoveTowards(-10f, 0, velocity * Time.deltaTime);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2019-03-26
@keksmr

hit - will store the last value anywhere. until it hits something.
physics in the unit has no concept - "collided with emptiness".
so you either null before each check (although I don’t remember if it works) or some one global collider/trigger ala “border of the world” to do)
well, in general, the logic of physics involves a reaction to “something that exists”. collisions, intersections, etc.)
try to write
what scrolls / moves / what it always does there, but stops if the beam crashes into something.

T
Tanya Levdik, 2020-05-05
@Rishel

To check if there is something in hit, you can comparehit.transform != null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question