REALLY NEED HELP How do I get a gun to fire when i press R2 on the ps4 controller?
Hi, I want a gun i have in my game to fire when I press R2 on the ps4 controller. Before, everything was set up to work using the mouse and WASD on the keyboard. I have managed to get the player to move but I am unsure how to get them to fire the gun with R2, everything is set up for the gun. I have played around with this for a while now and its just not working. Would appreciate some help.
I have a gun script where I have three functions: Shoot, AmmoDecrease and Reload.
public class Gun : MonoBehaviour
{
public float gundamage = 10f;
public float gunrange = 100f;
public Camera FPSCam;
public ParticleSystem MuzzleFlash;
public ParticleSystem MuzzleFlash1;
public ParticleSystem MuzzleFlash2;
public Light MuzzleLight;
public GameObject GunImpact;
public GameObject GunImpact1;
public int MaxAmmo = 10;
public int CurrentAmmo;
public float ReloadTime = 1f;
public AmmoAmount ammobar;
public bool IsReloading = false;
Ps4Controls control;
void Start()
{
CurrentAmmo = MaxAmmo; //current ammo amount is the maximum ammo at the start
}
void Update()
{
if (IsReloading) //player isn't reloading
return;
if (CurrentAmmo <= 0) //if ammo is less than 0, player needs to reload
{
Reload();
return;
}
if (Input.GetKey("Fire1")
{
Shoot();
AmmoDecrease(1);
UnityEngine.Debug.Log("fire");
}
}
public void AmmoDecrease(int ammonum)
{
CurrentAmmo -= ammonum;
ammobar.SetAmmo(CurrentAmmo);
}
void Reload()
{
if (Input.GetKey("Fire2")
{
IsReloading = true; //reloading is true
UnityEngine.Debug.Log("Reloading");
CurrentAmmo = MaxAmmo; //ammo is back at max
ammobar.SetAmmo(MaxAmmo); //ammo bar is set to max
}
IsReloading = false; //reloading is not happening anymore
}
public void Shoot()
{
MuzzleFlash.Play();
MuzzleFlash1.Play();
CurrentAmmo--; //if player shoots, ammo decreases
}
and this is what i have done for the player move script:
public class PlayerMove : MonoBehaviour
{
Ps4Controls control;
Vector3 move; //movement on x and y
void Awake()
{
control = new Ps4Controls();
control.GamePlay.PlayerMove.performed += ctx => move = ctx.ReadValue<Vector2>(); //reads value of the joystick player movement
control.GamePlay.PlayerMove.canceled += ctx => move = Vector3.zero; //sets joystick to 0, stops any movement
}
void Update()
{
transform.position += new Vector3(move.x * Time.deltaTime, 0, move.y * Time.deltaTime);
}
void OnEnable()
{
control.GamePlay.Enable();
}
void OnDisable()
{
control.GamePlay.Disable();
}
}
Your answer
Follow this Question
Related Questions
How to make text appear when button is highlighted with a controller? 0 Answers
Where are the FPSController prefab's Graphics and Main Camera children in Unity 5 0 Answers
First person controls for GoogleCardboard 2 Answers
BasicFPS controller "Can not be loaded" 0 Answers
How to get PS3 Controller working in OS X Yosemite? (Mac) 0 Answers