- Home /
C# ShootController()
hey guys. I have a problem with my ShootController(). Specifically when I play the game and press ("Fire1") for shooting my weapon shoots 3 bullets with one click. I cant imagine the problem So I decided to post it here.
Here is my script (I removed the parts which has nothing to do with Shooting):
//Sound variables
public GameObject bulletSound;
public GameObject holdSound;
public void Update()
{
ShootController();
}
public void ShootController()
{
if (Input.GetButton("Fire1"))
{
if (shoottime <= Time.time)
{
shoottime = Time.time + CurrentWeapon.firerate;
CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
}
GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
Physics.IgnoreCollision(inst_bullet.collider, CharCont);
inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
if (bulletSound)
{
holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
}
if (holdSound)
{
holdSound.transform.parent = transform;
}
}
}
public void RecoilController()
{
CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
RecoilHolder.localEulerAngles = CurrentRecoil2;
RecoilHolder.localPosition = CurrentRecoil4;
}
[System.Serializable]
public class WeaponInfo
{
public string name = "Weapon";
public float firerate = 0.1f;
public float bulletSpeed = 1;
public float weaponDamage = 10;
public Transform WeaponTransform;
public Transform Spawnpoint;
public Vector3 RecoilRotation;
public Vector3 RecoilKickback;
public GameObject Bullet;
public int CurrentScope;
public List<WeaponScope> Scopes = new List<WeaponScope>();
}
Answer by ReliCWeb · Jul 12, 2013 at 02:20 PM
Here's some pseudocode of how I did a machinegun in a recent prototype (m_fireRate = 0.1f):
if (Input.GetButton("Fire1"))
{
if (Time.time - m_lastFire > m_fireRate)
{
GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
Physics.IgnoreCollision(inst_bullet.collider, CharCont);
inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
if (bulletSound)
{
holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
}
if (holdSound)
{
holdSound.transform.parent = transform;
}
// Record fire time
m_lastFire = Time.time;
}
}
EDIT: Modified to include your bullet spawning code.
Sorry, wasn't sure what exactly you wanted. This shoots one bullet on a single click (assu$$anonymous$$g you release the button before m_fireRate), and allows continuous shooting if you hold the button down.
oh.. So, I have made a mistake.. this is what i want to do.. I tried to do it with your way but errors was being appeared. So I changed it a little bit :\ Could you replace "//shoot bullet code here, decrease bullets, etc" with codes?
Ok, I edited my original post to include your bullet spawning code. Also, after looking at your code again, yours should work if you put your bullet spawning code inside the "if (shoottime <= Time.time)" check of ShootController().
error CS0103: The name `m_lastFire' does not exist in the current context
What should I do now? is there any value to assing in order to make m_lastFire exist in the current context?
Answer by amphoterik · Jul 12, 2013 at 11:49 AM
I don't know what the value of CurrentWeapon.fireRate is, but you would want to use a different input method:
if (Input.GetButtonDown("Fire1"))
GetButtonDown returns true only if the button was just pressed. The one you used is GetButton which returns true every frame if the player holds the button.
yeah I changed it.. Now Im shooting One bullet/click i cant Shoot continuesly with my weapon :\ Would you like my full script? just to see what is going on with the other variables?
Wait, what are you trying to do? I thought the problem is that it shot continuously. What is the value of CurrentWeapon.fireRate?
Well.. Im making an FPS Game and i installed a ShootController Statement.. everything is fine but when i press FIRE1 it shoots 3 bullets with 1 click..
I also posted the full script (just wait to be verified)
Answer by george3d2011 · Jul 12, 2013 at 02:20 PM
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PlayerController : MonoBehaviour { public CharacterController CharCont; public CharacterMotor CharMotor; //Holder for Weapons public Transform WalkAnimationHolder; public Transform JumpAnimationHolder; public Transform ADSHolder; public Transform SwayHolder; public Transform RecoilHolder; public WalkingState walkingstate = WalkingState.Idle; public float VelocityMagnitude; public float WalkSpeed; public float RunSpeed; public bool WasStanding;
//Player Variables
public bool IsAiming;
public WeaponInfo CurrentWeapon;
public List<WeaponInfo> WeaponList = new List<WeaponInfo>();
public Vector3 CurrentRecoil1;
public Vector3 CurrentRecoil2;
public Vector3 CurrentRecoil3;
public Vector3 CurrentRecoil4;
private float shoottime = 0;
//Sounds
public GameObject bulletSound;
public GameObject holdSound;
void Start()
{
CurrentWeapon = WeaponList[0];
}
public void Update()
{
ShootController();
}
public void FixedUpdate()
{
AnimationController();
SwayController();
SpeedController();
RecoilController();
ADSController();
VelocityMagnitude = CharCont.velocity.magnitude;
}
public void SpeedController()
{
if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && VelocityMagnitude > 0)
{
if (Input.GetButton("Run"))
{
walkingstate = WalkingState.Running;
CharMotor.movement.maxForwardSpeed = RunSpeed;
CharMotor.movement.maxSidewaysSpeed = RunSpeed;
CharMotor.movement.maxBackwardsSpeed = RunSpeed / 2;
}
else
{
walkingstate = WalkingState.Walking;
CharMotor.movement.maxForwardSpeed = WalkSpeed;
CharMotor.movement.maxSidewaysSpeed = WalkSpeed;
CharMotor.movement.maxBackwardsSpeed = WalkSpeed / 2;
}
}
else
{
walkingstate = WalkingState.Idle;
}
}
public void AnimationController()
{
if(WasStanding && !CharCont.isGrounded)
{
WasStanding = false;
WalkAnimationHolder.animation.Play("animationSTANDINGJUMP");
}
else if (!WasStanding && CharCont.isGrounded)
{
WasStanding = true;
WalkAnimationHolder.animation.Play("animationSTANDINGJUMPLANDING");
}
if(walkingstate == WalkingState.Running)
{
WalkAnimationHolder.animation["animationRUN"].speed = VelocityMagnitude / RunSpeed * 1.2f;
WalkAnimationHolder.animation.CrossFade("animationRUN", 0.2f);
}
else if (walkingstate == WalkingState.Walking)
{
WalkAnimationHolder.animation["animationWALK"].speed = VelocityMagnitude / WalkSpeed * 1.2f;
WalkAnimationHolder.animation.CrossFade("animationWALK", 0.2f);
}
else
{
WalkAnimationHolder.animation.CrossFade("animationIDLE", 0.2f);
}
}
public void SwayController()
{
}
public void ShootController()
{
if (Input.GetButton("Fire1") && walkingstate != WalkingState.Running)
{
if (shoottime <= Time.time)
{
shoottime = Time.time + CurrentWeapon.firerate;
CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
}
GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
Physics.IgnoreCollision(inst_bullet.collider, CharCont);
inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
if (bulletSound)
{
holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
}
if (holdSound)
{
holdSound.transform.parent = transform;
}
}
}
public void RecoilController()
{
CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
RecoilHolder.localEulerAngles = CurrentRecoil2;
RecoilHolder.localPosition = CurrentRecoil4;
}
public void ADSController()
{
if (Input.GetButton("Fire2"))
{
IsAiming = true;
ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope = 0].adsposition, 0.25f);
}
else
{
IsAiming = false;
ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, Vector3.zero, 0.25f);
}
}
}
public enum WalkingState { Idle, Walking, Running, }
[System.Serializable] public class WeaponInfo { public string name = "Weapon"; public float firerate = 0.1f; public float bulletSpeed = 1; public float weaponDamage = 10; public Transform WeaponTransform; public Transform Spawnpoint; public Vector3 RecoilRotation; public Vector3 RecoilKickback; public GameObject Bullet; public int CurrentScope; public List Scopes = new List(); }
[System.Serializable] public class WeaponScope { public string name; public float fov; public Vector3 adsposition; public Transform scopetransform; }
Your answer

Follow this Question
Related Questions
Problem creating a 2D scroller shooting game 2 Answers
Bullet not moving 1 Answer
Troubles With A Shoot Script 1 Answer
Need a script for a gun that shoots bullet using raycast 2 Answers
Destroy Turret with machine Gun 0 Answers