自动射击代码

No replies
u1
User offline. Last seen 1 周 2 天 ago. Offline
初级会员
Joined: 09/07/2009
Points: 1210

 I made a script for someone in that past, that incorporated animations and projectiles!! here it is!!
代码如下:

var reload : boolean = false;

function Awake () {

animation["fire"].speed = 2;

animation["idle"].wrapMode = WrapMode.Loop;

animation["fire"].wrapMode = WrapMode.Clamp;

animation["reload"].wrapMode = WrapMode.Clamp;

animation.Play("idle", PlayMode.StopAll);

bullets = clipSize;

}

function Update () {

if (clips >= 0) {

if (Input.GetKey("e") && bullets >= 1 && animation.IsPlaying("idle")) {

SendMessage("Fire");

}

else if (bullets <= 0 && !reload) {

reload = true;

SendMessage("Reload");

SendMessage("InvokeReload");

}

else if (!animation.isPlaying) {

SendMessage("Idle");

}

}

if (bullets > clipSize) {

bullets = clipSize;

}

if (clips <= 0) {

SendMessage("Idle");

}

}

function Fire () {

animation.Play("fire");

bullets -= 1;

var Projectile : Rigidbody = Instantiate(projectile, barrel.position, barrel.rotation);

Projectile.velocity = transform.TransformDirection(Vector3 (speed, 0, 0));

 

if (fireSound)

AudioSource.PlayClipAtPoint(fireSound, transform.position);

}

function Reload () {

animation.Play("reload", PlayMode.StopAll);

yield WaitForSeconds(animation["reload"].length);

bullets = clipSize;

reload = false;

}

function Idle () {

animation.Play("idle");

}

function InvokeReload () {

clips -= 1;

}

function OnGUI () {

GUI.Box( Rect(Screen.width - 110,Screen.height - 30, 100, 25), "Clips left " + (clips));

GUI.Box( Rect(Screen.width - 110,Screen.height - 70, 100, 25), "Bullets left " + (bullets));

}
提示:
 NOTE: this script requires a reload animation, fire animation, and idle animation.