A bullet is the origin, emitting a ray, and if the ray detects an obstacle, it returns the collision point of the ray and the obstacle.
Instantiate a bullet hole map at this point
1 voidUpdate () {2Transform. Translate (Vector3.forward * speed *time.deltatime);3 //the collision of a bullet, measured by a ray of light, is counted as a collision when the object is within a ray produced between two points .4 //1. Get the starting point of the Ray (origin)5 //2. Get the direction of the Ray6 //3. Distance from the Ray7Vector3 oripos = transform.position;//1. Beginning of the Ray8Vector3 direction = Transform.position-oripos;//2. Direction9 floatEngth = (transform.position-oripos). magnitude;//3. The distance of the ray, the size of the orientation amountTen //Ray Casting Collision OneRaycasthit Hitinfo;//Storing collision Information A //light projection to detect collisions - BOOLIscillider = Physics.raycast (Oripos,direction, outhitinfo,ength); - the - //Create a bullet hole map at a collision point - if(iscillider) { - //The Ray detects the object and performs the following actions + intindex = Random.range (0,2);//Randomly generated 0| | 1 -Gameobject Bulletholeprefab =Bulletholes[index]; +Vector3 pos = hitinfo.point;//Impact Point APos.y + =0.5f;//move up to prevent overlap with object planes atGameobject go = gameobject.instantiate (bulletholeprefab,pos,quaternion.identity) asGameobject; - //Adjust the orientation of the map - //Map the bullet holes to the plane of the object, parallel to the plane -Go.transform.LookAt (Hitinfo.point-hitinfo.normal); - } -}
"Unity3d" Shootout game-Bullet hole settings