Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TallerAnts · Feb 27 at 08:47 PM · unity 2dscripting beginnerprefabsgameobject.find

How to instantiate a player's weapon after picking it up?

I want to build a quick and simple prototype where I have a weapon sprite on the ground, the square walks to it, and it picks up the gun, making it usable. I have a shooting script that works on the gun prefab, but needs variables to be dragged and dropped. My gun prefab that is spawned already attached to the player needs the camera, gun gameobject transform, as well as the bullet prefab and the UI element which references the ammo HUD.

I want to make a system where I can pick up a gun prefab and have it equipped to shoot, but instantiating the gun prefab requires the drag and dropping of the aforementioned gameobjects in order for the script to function. Is there a solution that does not require the prefab variable drag and dropping?

Shooting script:`[SerializeField] private int ammo; [SerializeField] private Text ammoDisplay; [SerializeField] private Transform fireP; [SerializeField] private float fireRate = 0.3f; [SerializeField] private float nextFire = 0f; public bool isFiring; [SerializeField] private GameObject bulletPF;

 [SerializeField] private float bulletForce = 20f;
 private void Start(){
     fireP = GameObject.Find("Fire Point").transform;
 }
 // Update is called once per frame
 void Update(){
     ammoDisplay.text = ammo.ToString();
     if (Input.GetMouseButton(0) && !isFiring && Time.time > nextFire && ammo > 0){
         nextFire = Time.time + fireRate;
         Shoot();
         ammo--;
     }
     if (Input.GetKeyDown(KeyCode.R)){
         ammo = 6;
         nextFire = 0f;
     }
 }
 void Shoot(){
     isFiring = true;
     GameObject bullet = Instantiate(bulletPF, fireP.position, fireP.rotation);
     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(fireP.up * bulletForce, ForceMode2D.Impulse);
     isFiring = false;
 }

` Arm Cam script:

 Vector2 mousePos;
     public Camera cam;
     public float slerpSpeed = 20f;
     public Transform tr;
     // Update is called once per frame
     void Update(){
         cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
         tr = GetComponent<Transform>();
         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
     }
     private void FixedUpdate(){
         Vector2 lookDir = mousePos - (Vector2)tr.position;
         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         tr.rotation = Quaternion.Slerp(transform.rotation,rotation, slerpSpeed*Time.deltaTime);
      }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by VonKiver · Feb 28 at 02:19 PM

Maybe something like a pickup system? You have a game object which contains the prefab of weapon you want to equip.

 public class Pickup : MonoBehaviour
     {
         public Weapon weaponToEquip;
     
         private void OnTriggerEnter2D(Collider2D collision)
         {
             if(collision.tag == "Player")
             {
                 collision.GetComponent<Player>().ChangeWeapon(weaponToEquip);
                 Destroy(this.gameObject);
             }
         }
     }


And in the player script you have something like:

 public Transform weaponPosition;
 
 public void ChangeWeapon(Weapon weaponToEquip)
     {
         Destroy(GameObject.FindGameObjectWithTag("Weapon"));
         Instantiate(weaponToEquip, weaponPosition.position, transform.rotation, transform);
     }



I hope this helps. :)
EDIT: I just read your question again and I imagine this is exactly what you didn't want as a solution... however this is the only way I can think of. Sorry if it didn't help :/

Comment
Add comment · Show 2 · Share
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 TallerAnts · Mar 01 at 03:39 AM 1
Share

Hi, thanks for the reply, I implemented a guns manager gameobject to my player gameobject which manages all detected weapons. It actually works like a pickup system as you mentioned, which is perfect. Thank you!

avatar image VonKiver TallerAnts · Mar 01 at 06:06 AM 0
Share

Nice. I'm glad you've figured it out!

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

159 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

Related Questions

How to spawn multiple prefabs - Tilemap 0 Answers

How do I make it so every object the player creates (in a building game) gets a script attach to it by default? 4 Answers

creating and using list of prefabs 2 Answers

instantiating unique classes of a type 0 Answers

Is there a way to protect a prefab variable values? (Anti cheat) 2 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