Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Gabimela · Oct 13, 2020 at 07:06 PM · unity 5controllerinputmanagerinputsplaystation

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();
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

319 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges