Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by D0minick · Aug 19, 2019 at 07:11 AM · 2d-platformershootingoncollisionenterpick up

I am trying to change the bulletPrefab OnCollisionEnter2D but its not working now that I changed to fire at mouse position

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
   
 
 public class Weapon1 : MonoBehaviour
 {
 
     public PauseMenuScript GameIsPaused;
 
     //public Transform firePoint;
     GameObject bulletPrefab;
     public GameObject fastBullet;
     public GameObject slowBullet;
     public GameObject standardBullet;
 
     public float duration = 10f;
     public float bulletVelocity = 20f; //new code for mouse position firing
     public float fastbulletVelocity = 60f;
     public float slowbulletVelocity = 10f;
 
     public int maxAmmo = 50;
     public int currentAmmo;
     public float reloadTime = 1.5f;
     private bool isReloading = false;
     private bool cooldown = false;
 
     public GameObject ammo;
     public Text currentAmmoText;
     public Text maxAmmoText;
     public bool ReadyFire = true;
 
     void Start()
     {
         currentAmmo = maxAmmo;
     }
 
     // Update is called once per frame
     void Update()
     {
         currentAmmoText.text = currentAmmo.ToString();
         maxAmmoText.text = maxAmmo.ToString();
 
         if (isReloading)
             return;
 
         Shoot();
 
         Fastshoot();
 
         Slowshot();
 
         /*if (currentAmmo <= 0)
         {
             StartCoroutine(Reload());
             return;
         }*/
 
         if (Input.GetKeyDown(KeyCode.R))
         {
             bulletPrefab = standardBullet;
             Debug.Log("Bullet type set back to standard.");
         }
 
         if (currentAmmo > maxAmmo)
         {
             currentAmmo = maxAmmo;
         }
 
         if (currentAmmo > 0)
         {
             ReadyFire = true;
         }
 
         if (currentAmmo <= 0)
         {
             ReadyFire = false;
         }
 
         if (currentAmmo <= 0 && Input.GetButtonDown("Fire1") && Time.timeScale > 0)
         {
             SoundManager.PlaySound("NoAmmoSound");
         }
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.CompareTag("FastBulletPickup"))
         {
             bulletPrefab = fastBullet;
             Destroy(GameObject.FindGameObjectWithTag("FastBulletPickup"));
             Debug.Log("Fast bullets activated.");
             StartCoroutine (Fast(other));
             //ammo.GetComponent<Image>().color = Color.green;
         }
 
         if (other.CompareTag("SlowBulletPickup"))
         {
             bulletPrefab = slowBullet;
             Destroy(GameObject.FindGameObjectWithTag("SlowBulletPickup"));
             Debug.Log("Slow bullets activated.");
             StartCoroutine(Slow(other));
             //ammo.GetComponent<Image>().color = Color.blue;
         }
 
         /*
         else
         {
             Shoot();
         }
         */
 
         /*if (other.CompareTag("FastBulletPickup"))
         {
             bulletPrefab = fastBullet;
             Destroy(GameObject.FindGameObjectWithTag("FastBulletPickup"));
         }
 
         if (other.CompareTag("SlowBulletPickup"))
         {
             bulletPrefab = slowBullet;
             Destroy(GameObject.FindGameObjectWithTag("SlowBulletPickup"));
         }*/
     }
 
     IEnumerator Fast(Collider2D player)
     {
         if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
         {
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
             //currentAmmo--;
 
             Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 direction = (Vector2)((worldMousePos - transform.position));
             direction.Normalize();
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
             GameObject bulletPrefab = (GameObject)Instantiate(fastBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
             bulletPrefab.GetComponent<Rigidbody2D>().velocity = direction * fastbulletVelocity; // new code for fire from mouse position
             SoundManager.PlaySound("WeaponSound");
             currentAmmo -= 1;
             Invoke("ResetCooldown", 1.5f);
             cooldown = true;
         }
 
         if (GameIsPaused == true)
         {
             CancelInvoke();
         }
 
         //GetComponent<SpriteRenderer>().enabled = false;
         //GetComponent<BoxCollider2D>().enabled = false;
 
         yield return new WaitForSeconds(duration);
 
         //bulletPrefab = standardBullet;
 
         //ammo.GetComponent<Image>().color = Color.magenta;
     }
 
     void ResetCooldown()
     {
         cooldown = false;
     }
 
     IEnumerator Slow(Collider2D player)
     {
         if (Input.GetButtonDown("Fire") && Time.timeScale > 0 && ReadyFire == true)
         {
             Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 direction = (Vector2)((worldMousePos - transform.position));
             direction.Normalize();
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
             GameObject bulletPrefab = (GameObject)Instantiate(slowBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
             slowBullet.GetComponent<Rigidbody2D>().velocity = direction * slowbulletVelocity; // new code for fire from mouse position
             SoundManager.PlaySound("WeaponSound");
             currentAmmo -= 1;
 
             /*if (GameIsPaused == true)
             {
                 CancelInvoke();
             }*/
         }
 
         //GetComponent<SpriteRenderer>().enabled = false;
         //GetComponent<BoxCollider2D>().enabled = false;
 
         yield return new WaitForSeconds(duration);
 
         bulletPrefab = standardBullet;
 
         ammo.GetComponent<Image>().color = Color.magenta;
     }
 
     void Shoot()
     {
         if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
         {
             Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 direction = (Vector2)((worldMousePos - transform.position));
             direction.Normalize();
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
             GameObject bulletPrefab =
                 (GameObject)Instantiate(
                     standardBullet,
                     transform.position +
                     (Vector3)(direction * 2f),
                     Quaternion.identity); //new code fire based off mouse position
             bulletPrefab.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity; // new code for fire from mouse position
             SoundManager.PlaySound("WeaponSound");
             currentAmmo -= 1;
             Invoke("ResetCooldown", 1.5f);
             cooldown = true;
         }
 
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             CancelInvoke();
             Time.timeScale = 0f;
         }
     }
 
     void Fastshoot() //used to test different bullet types
     {
         if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
         {
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
             //currentAmmo--;
 
             Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 direction = (Vector2)((worldMousePos - transform.position));
             direction.Normalize();
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
             GameObject bulletPrefab = (GameObject)Instantiate(fastBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
             bulletPrefab.GetComponent<Rigidbody2D>().velocity = direction * fastbulletVelocity; // new code for fire from mouse position
             SoundManager.PlaySound("WeaponSound");
             currentAmmo -= 1;
             Invoke("ResetCooldown", 1.5f);
             cooldown = true;
         }
 
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             CancelInvoke();
             Time.timeScale = 0f;
         }
     }
 
         void Slowshot() //used to test different bullet types
         {
             if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
             {
             Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 direction = (Vector2)((worldMousePos - transform.position));
             direction.Normalize();
             //Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
             GameObject bulletPrefab = (GameObject)Instantiate(slowBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
             slowBullet.GetComponent<Rigidbody2D>().velocity = direction * slowbulletVelocity; // new code for fire from mouse position
             SoundManager.PlaySound("WeaponSound");
             currentAmmo -= 1;
             Invoke("ResetCooldown", 1.5f);
                 cooldown = true;
 
                 /*if (GameIsPaused == true)
                 {
                     CancelInvoke();
                 }*/
             }
 
             if (Input.GetKeyDown(KeyCode.Escape))
             {
                 CancelInvoke();
                 Time.timeScale = 0f;
             }
         }
         IEnumerator Reload ()
         {
             isReloading = true;
 
             Debug.Log("Reloading...");
 
             yield return new WaitForSeconds(reloadTime);
 
             currentAmmo = maxAmmo;
 
             isReloading = false;
         }
 }

But the issue I am having is that even when I get the pickups from OnCollisionEnter2D I can no longer get the prefab to change, compared to when I used to have the character just shoot from a set firepoint, any help would be greatly appreciated.

Comment
Add comment · Show 1
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
avatar image D0minick · Aug 19, 2019 at 12:37 AM 0
Share

fixed it with a bool, thank you anyway guys

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

120 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

Related Questions

OnCollisonEnter2D. 1 Answer

OnCollisionEnter2D ignores Z axis!,OnCollissionEnter2D ignores z axis. 0 Answers

shooting in 2-d 1 Answer

3 Directional Shooting in 2D Platformer 0 Answers

How can I make an object invulnerable to collision with a specific object? 1 Answer


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