A tool that is very practical and often useful for ray detection in unity. Here's a look at the ray detection problem.
1) Ray
Defines a ray based on the direction of the ray's endpoints and rays
New Ray (Transform.position, Transform.forward);
Define a variable Raycasthit hit with ray projection information and perform a radiographic inspection physics.spherecast
Raycasthit hit;
if (Physics.spherecast (Ray,1f,out hit))
{
if (hit.distance<3f)
{
float angle = Random.range (90, 110);
print (angle);
Transform. Rotate (0, angle, 0);
}
}
A hit can be obtained by using the structure variable hits to obtain the relevant data after the ray touches the object. Point (collision dot coordinate), Hit.transform.gameObject (Collision object, which in turn can get the script component on the game object, which can call the method or variable on the component);
Ray detection can be used raycast can also be used spherecast, specific what type of ray projection can be selected according to the needs of their own
1) Screen Ray
The screen Ray is a ray with a camera.
Ray Ray = Maincamera.screenpointtoray (point); Raycasthit hit; if out Hit )) { Startcoroutine (createonshow (Hit.point)); = Hit.transform.gameObject; = Hitgobj.getcomponent<reacttohit>(); if (newreactor!=null) { newreactor.react (); } }
Unity Ray Detection