First, the collision detection of bullets:
Because bullets move very fast, if you add a collider to a bullet, you may not be able to detect it.
Because the collider is executed at every frame, the first frame of the bullet may be 100 meters, then the next frame is 900 meters,
Then the middle barrier cannot be detected.
Therefore, radiographic testing is required:
At each frame of execution, a ray is emitted up a frame, continuously detecting the presence of a barrier object that passes through the ray.
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); - if(iscillider) { the //The Ray detects the object and performs the following actions
}
Second, the generation of bullets
The first thought was to generate bullets at the muzzle and then move the bullets.
If this is the case, the player cannot refer to which fight
So it needs to be in the center of the screen to generate bullets.
Still using the rays to move the bullets in the direction of the beam.
Get the center of the screen as a Ray origin.
Emission Ray
If the ray collides with the object, return to that point, determine the origin and direction of the point, and the bullet moves in that direction
If not, the target point moves forward a certain distance from the origin, as the target point, determines the direction, and the bullet moves in that direction
1 //start shooting .2 voidShoot () {3 //Flash4 Flashcol. Flash ();5 //Generate Bullets6 //gameobject.instantiate (Bulletprefab, fllow.transform.position, fllow.transform.rotation);7Gameobject go = gameobject.instantiate (bulletprefab,transform.position,quaternion.identity) asGameobject;8 //get the center position of the screen and convert to world coordinates9Vector3 point = Solidercamera.screentoworldpoint (NewVector3 (Screen.width/2, Screen.height/2,0));Ten //define Ray One Raycasthit Hitinfo; A //emission Ray - BOOLIscollider = Physics.raycast (Point, SoliderCamera.transform.forward/*Camera forward*/, outhitinfo); - if(iscollider) { the //If the ray hits the object, - go.transform.LookAt (hitinfo.point); -}Else { - //If the ray has no impact object + //so the target point is 1000 meters away. -Point + = SoliderCamera.transform.forward * +; + go.transform.LookAt (point); A } at}
"Unity3d" shootout game-firing bullets, radiographic testing