U
U
Uncle Bogdan2021-07-13 12:07:32
Unity
Uncle Bogdan, 2021-07-13 12:07:32

How to shoot a beam from one object in the direction of another?

Tipo determine the distance, but at the same time so that if there is a wall in front, then he would not go.

60ed57cef2f36980989467.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
ReyGrau, 2021-07-13
@ReyGrau

You can use Distance to determine the distance, and let Raycast in the direction of travel to check the obstacle.

B
B1tH0ven, 2021-07-13
@B1tH0ven

Raycast requires Ray and distance as arguments (A number of other attributes are possible, but they are not very relevant to this topic).
Ray, on the other hand, requires you to specify a start point and direction when creating it. (Also, you can specify the start point and direction in Raycast instead of Ray).
In your case, the start point is the position of the first object, the direction is the subtraction of the position of the second from the position of the first (secondObject.transform.position - firstObject.transform.position). If the distance to the target is not important, you can write Mathf.Infinity in its place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question