Hey guys! First of all, thanks for any help given. Second, I’ve just gotten back into Unity (I haven’t used it in a while), so I’m really rusty with it right now. Also, this is my first time posting on the forums, so please forgive me for any formatting errors. Ok, so I’m making a standard runner game with firing at enemies. The character moves forward on it’s own, and the moving side-to-side works, too. The only thing that isn’t working at this stage is the firing. It works whenever I use Input.GetButton(“Fire”), but it fires too many bullets. I only want to fire one bullet per button-press. Here’s the code:
#pragmastrict
varforwardSpeed = 5.0;
varsideSpeed = 5.0;
privatevarmoveDirection : Vector3;
varbullet : Rigidbody;
varforwardOffset = 1.5;
varbulletSpeed = 60;
vargravity = 20.0;
varlaserSound : AudioClip;
functionStart () {
}
functionUpdate () {
varcontroller : CharacterController = GetComponent(CharacterController);
if(Input.GetButton(“Left”))
{
moveDirection = Vector3(-sideSpeed, 0, 0);
}
if(Input.GetButton(“Right”))
{
moveDirection = Vector3(sideSpeed, 0, 0);
}
if((Input.GetButtonUp(“Left”))||(Input.GetButtonUp(“Right”)))
{
moveDirection = Vector3(0, 0, 0);
}
moveDirection.z = forwardSpeed;
moveDirection.y = -gravity;
controller.Move(moveDirection*Time.deltaTime);
if(Input.GetButton(“Fire”))
{
varbulletClone = Instantiate(bullet, transform.position+transform.forwardforwardOffset, transform.rotation);
bulletClone.velocity = transform.forwardbulletSpeed;
}
}
functionOnControllerColliderHit(hit : ControllerColliderHit)
{
varbody : Rigidbody = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if (hit.moveDirection.y < -0.3)
return;
varpushDir : Vector3 = Vector3(moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushDir*forwardSpeed;
}