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)