Game Development Reference
In-Depth Information
This function also performs a physics operation known as a raycast. A raycast
sends out a ray of collision information and checks for objects that intersect with
the ray. This is a useful technique for creating a gun that shoots bullets. Were the
designer to use a gun script that instantiates and launches bullet objects, the bullet
may miss an enemy completely despite being fired straight at it. This would occur
because the bullet would be rendered every frame at its location as determined by the
speed it was traveling. If the enemy is located between where the bullet is rendered
at frame 30, for example, and where it is at frame 31, the collider on the bullet never
technically hits the collider on the enemy.
If the ray does hit something, this script also spawns particles at the place it hits
to create a shooting effect. If this object has an ApplyDamage function in its script, it
also sends that message and causes damage according to the damage variable. If it is
found that bulletsLeft , the bullets left in the clip, equals 0 and that extraBullets , the
player's reserve ammo, is greater than 0, the Reload function is called.
function FireOneShot () {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
GameObject.Find(“HandgunRig”).animation.Play(“Fire”);
GameObject.Find(“HandgunRig”).animation[“Fire”].speed = 2;
if (Physics.Raycast (transform.position, direction, hit, range)) {
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = r
Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
if (hit.collider == true) {
hit.collider.SendMessageUpwards(“ApplyDamage”, damage, r
SendMessageOptions.DontRequireReceiver);
}
hit.collider.SendMessageUpwards(“ApplyDamage”, damage, r
SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
m_LastFrameShot = Time.frameCount;
enabled = true;
if (bulletsLeft == 0 && extraBullets > 0) {
Reload();
}
}
Search WWH ::




Custom Search