Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Instantiate bullet once when virtual joytick input is over 0.5?

Discussion in 'Scripting' started by Olexji, Dec 7, 2017.

  1. Olexji

    Olexji

    Joined:
    Oct 20, 2015
    Posts:
    2
    Hi,
    when my input is between 0f and 0.49f I can rotate the player with my joystick without shooting the bullets. BUT my Problem is that the amount of bullets are way to much when the input of my joystick is >= 0.5f || <= -0.5f, I just want 1 bullet to spawn than wait a little time until the bullet can be spawn again. How would you go about this? by the way the timeStamp-part doesnt really work

    this is in my update()
    Code (CSharp):
    1. float shootHorizontal = CrossPlatformInputManager.GetAxis ("shootHorizontal");
    2.         float shootVertical = CrossPlatformInputManager.GetAxis ("shootVertical");
    3.         shootInput = new Vector3 (shootHorizontal, 0f, shootVertical);
    4.         //change 1f to smooth rotation
    5.         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (shootInput), 1f);
    6.         if (shootHorizontal >= 0.5f || shootHorizontal <= -0.5f && Time.time >= timeStamp ) {
    7.             //spawn bullets
    8.             ShootBullets();
    9.             timeStamp = Time.time + timeBetweenShots;
    10.         }
    11.         if (shootVertical >= 0.5f || shootVertical <= -0.5f && Time.time >= timeStamp ) {
    12.             ShootBullets ();
    13.             timeStamp = Time.time + timeBetweenShots;
    14.         }
    Code (CSharp):
    1. void ShootBullets(){
    2.         var bullet = (GameObject)Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
    3.  
    4.         bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 6;
    5.     }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think you want extra parenthesis in there like:
    Code (csharp):
    1.  
    2. if ((shootHorizontal >= 0.5f || shootHorizontal <= -0.5f )&& Time.time >= timeStamp )
    3.  
    4.  
    I could be wrong, but other than that, it looks like it would work to me.
     
    Last edited: Dec 7, 2017
  3. Olexji

    Olexji

    Joined:
    Oct 20, 2015
    Posts:
    2
    @fire7side thanks for your help, you were right :D

    i changed the script a little bit for my needs but the concept is still the same
    Here is the final method, if someone wants to use it.
    put this in the update method and change the fireRate to your needs

    Code (CSharp):
    1. waitTilNextFire -= Time.deltaTime * fireRate;
    Code (CSharp):
    1. void ShootBullets(){
    2.         float shootHorizontal = CrossPlatformInputManager.GetAxis ("shootHorizontal");
    3.         float shootVertical = CrossPlatformInputManager.GetAxis ("shootVertical");
    4.         shootInput = new Vector3 (shootHorizontal, 0f, shootVertical);
    5.         //change 1f to smooth rotation
    6.         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (shootInput), 1f);
    7.  
    8.  
    9.         if((shootHorizontal >= 1f ||  shootVertical >= 1f)  && waitTilNextFire <= 0f){
    10.  
    11.             //var bullet = (GameObject)Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
    12.             var bullet = (GameObject)Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
    13.             bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 6;
    14.  
    15.             waitTilNextFire = 1f;
    16.  
    17.         }
    18.         if ((shootHorizontal <= -1f || shootVertical <= -1f) && waitTilNextFire <= 0f) {
    19.             var bullet = (GameObject)Instantiate (bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
    20.             bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * 6;
    21.  
    22.             waitTilNextFire = 1f;
    23.         }