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 unity_hxq_CuIpXNWBGA · Apr 11, 2020 at 01:53 PM · transformparent

Unity 3D: How to pick up object and attached it to player on TriggerEnter?

I want my player to carry the axe with him when he touched it (see photo below 1.png). I know it has to do with the transform the parent thing but I can't seem to understand tutorials about it. I only figured out how to destroy the axe when I approach it and create another prefab of the axe near my player, but the axe just stays where it is and doesn't follow me (hence what I said earlier on how I can't understand the transform parent). I know what parent and child are (I actually created my player with the camera that follows him with his movement). Here's what I coded so far:

 public Transform Spawnpoint;
 public GameObject Prefab;


 void OnTriggerEnter(Collider collider)
 {
     if (collider.gameObject.tag == "Player")
     {
         Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation);
         Destroy(gameObject);
     }
 }

}

1.png (413.0 kB)
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
Best Answer

Answer by sujitmarcus · Apr 11, 2020 at 02:11 PM

You Could do this in various way.. 1. Your player could have array of weapons from which he can only have one active at a time. and OnTriggerEnter could trigger the function.

Attach this on player Object.

For example

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player_WeaponManager : MonoBehaviour
 {
 
     public GameObject[] Weapons;
     public GameObject _currentWeapon;
 
 
     public Transform Spawnpoint;
     public GameObject Prefab;
     // Start is called before the first frame update
     void Start()
     {
         //Assigning default Weapon
         // Weapons[0] could be some other wheapon
         SwitchWeapom(0);
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
 
     public  void SwitchWeapom(int index)
     {
         _currentWeapon = Weapons[index];
         //instantiate Current Weapon
         GameObject obj =  Instantiate(_currentWeapon, Spawnpoint.position, Spawnpoint.rotation);
         obj.transform.parent = this.transform;
 
     }
 
  
     void OnTriggerEnter(Collider collider)
     {
         if (collider.gameObject.tag == "AxePickup")
         {
             Destroy(collider.gameObject);
             // On weapons array 2 could be the axe
             SwitchWeapom(2);
         }
     }
 }

Comment
Add comment · 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

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

160 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

Related Questions

How to transform parent object from child? 1 Answer

Fixing pivot points with second gameObject workaround 1 Answer

Kinematic Child Object Jumping Above Player 1 Answer

moving child objects in hierarchy and keep stacks/sorting? Help needed 0 Answers

Howto set main.camera as a parent when main.camera itself is a child of a prefab 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