Physics.raycast: Ray projection
Parameters:
Origin: Ray start point
direction: Ray Direction
Distance: Ray length
Layermask: only The collider within the Layermask layer is selected, and the other in-layer collider is ignored.
Returns
Bool-true when the ray intersects any collider,otherwise false.
True if Ray casts intersect with any collider, otherwise false.
Here's a small example to experiment with: Click where you want to move
usingUnityengine;
usingSystem.Collections;
Public classCapsule:Monobehaviour {
Public Charactercontrollercc;
Public floatSpeed ;
Public Cameracamera;
Public Vector3targetposition;
voidStart () {
targetposition= This. transform.position; // Assign the initial position to the current coordinates
cc= This.gameobject.getcomponent<Charactercontroller> ();
}
To define a ray method:
voidGethitpoint () {
RayRay; // Create a new Ray ,
RaycasthitHit ; // Ray casting clash Qi
if(Input. Getmousebuttondown (0)){ // If I click on the screen to trigger the Ray
Ray=camera. Screenpointtoray (Input. mouseposition); // screen position turn Ray
if(Physics. Raycast (Ray, outHit , -)){// Ray projection -- true when Ray casts intersect with any collider , the Ray Distance
//Debug.Log (hit.point);
if(hit.collider.gameobject.name=="Terrain"){
Targetposition=hit.point;
//debug.log (targetposition);
}
}
}
}
voidWalk () {
Vector3v=Vector3. Normalize (targetposition- This. transform.position);
cc. Simplemove (v*speed* Time. deltatime);//simple Move method.
}
voidUpdate () {
//Vector3 v=new Vector3 (0,0,10*speed*time.deltatime);
if (Input.getkey (Keycode.a)) {
Cc. Simplemove (v);
// }
walk ();
gethitpoint ();
}
}
X-ray collision test sharing in Unity: