- Home /
2D sidescroller shooting
I made the arm rotate using wasd using this code:
public class ArmRotation : MonoBehaviour {
public int armRot = 0;
// Use this for initialization
// Update is called once per frame
void Update () {
if(Input.GetButton("AimUp")&&Input.GetButton("AimSide"))
armRot = 45;
else if (Input.GetButton("AimDown"))
armRot = -45;
else if (Input.GetButton("AimUp"))
armRot = 90;
else armRot = 0;
transform.rotation = Quaternion.Euler(0f,0f, armRot );
}
}
but i can't figure out how to make my gun shoot properly i tried this but it doesn't work :
public class WeaponScript : MonoBehaviour {
public float fireRate = 0;
public float Damage = 10;
public LayerMask notToHit;
float timeToFire = 0;
Transform firePoint;
Transform firePoint2;
// Use this for initialization
void Awake () {
firePoint = transform.FindChild ("FirePoint");
if (firePoint == null){
Debug.LogError ("FirePoint not found");
}
firePoint2 = transform.FindChild ("FirePoint2");
if (firePoint == null){
Debug.LogError ("FirePoint2 not found");
}
}
// Update is called once per frame
void Update () {
if (fireRate == 0){
if (Input.GetButtonDown("Fire")){
Shoot();
}
}
else {
if (Input.GetButtonDown("Fire")&& Time.time > timeToFire){
timeToFire = Time.time + 1/fireRate;
Shoot();
}
}
}
void Shoot (){
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
Vector2 firePointPosition2 = new Vector2 (firePoint2.position.x, firePoint2.position.y);
Debug.DrawLine (firePointPosition, firePointPosition2, Color.red);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, firePointPosition2- firePointPosition);
}
}
where firePoint is a empty game object next to the barrel and firePoint2 is a empty game object located in front of the barrel (by 0.3 units)
Answer by Eudaimonium · Sep 06, 2015 at 09:14 PM
The logic for determining if you can shoot in your Update method is... ehh, FUBAR.
Basically if you just want to have a delay between firing, simply use this:
public float RefireDelay = 1f;
private float _refireDelay;
public void Start()
{
_refireDelay = RefireDelay;
}
public void Update()
{
_refireDelay -= Time.deltaTime;
if (_refireDelay < 0)
{
Shoot();
_refireDelay = RefireDelay;
}
}
Keep your Shoot functions, and all public variables and transforms.
Don't use Time.time, use deltaTime, that's a time difference that elapsed between last frame and this one.
When comparing float values, never use == operator because it will never return true. Your float timers will never be EXACTLY 0 values, it will go from 0.005123 to -0.0110293 or something like that. So always check less or more, never equal, with time and other floating point values.
Your answer
Follow this Question
Related Questions
How to make a simple line of sight in a 2D top down shooter. 3 Answers
Shooting in direction of mouse cursor 2d 5 Answers
Raycast Physics2D not working as expected 1 Answer
[2D] Find nonobscured corners of 2D sprites or collision boxes? 0 Answers
2d Raycasting, trouble drawing rays at the correct angles 1 Answer