"Unity3d" Shootout game-Bullet hole settings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.