• 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 Neferlem · May 14, 2020 at 07:19 PM · animationprefabinstantiate prefab

Animate an instantiated Prefab property

Hi everyone,

My question is maybe a bit complex, but i'll try to give all the details that I can. In my game, I made a system for instantiating prefabs that will be used as player weapon.

First, how I manage my prefab : The player walks in the trigger zone of a weapon that is on ground, he picks it up, and the weapon goes in player hands, by instantiating a prefab that is spawned inside hierarchy of player model.

Second, I need to manage the box collider of the weapon during animation of player. The main problem is that box collider is always on, when I walk towards an enemy, the weapon triggers a damage on him without attacking. So, I wanted to disable box collider when the player is not attacking (idle animation, walking, etc.) So I used keyframes on player animation, but it goes weird since I'm trying to change box collider of an instantiated prefab.

I think that property showing inside animation window is the one of the previous runtime instantiated prefab, when I reload the party, It cannot works as it will instantiate another prefab... What is the solution here? Thanks!

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
0
Best Answer

Answer by Neferlem · May 27, 2020 at 07:03 PM

All right, I found a way when looking for info on Unity docs.

I attached a script on the prefab supposed to spawn in the player hands. Here it is :

  public Animator playerAnimator;
     private BoxCollider2D weaponBoxCol; //I declare the collider of the weapon itself    
 
     void Start()
     {
         playerAnimator = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>(); //I'm getting the Player Animator
         weaponBoxCol = GetComponent<BoxCollider2D>(); //I get the collider of the weapon
         weaponBoxCol.enabled = false; //The collider starts disabled
     }
 
     void Update()
     {
         if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("player_weapon")) //I check if the animation played is the one when player swings weapon
         {
             if (playerAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0 && playerAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 0.5) //If the animation is between the beginning and middle of it, the collider stays disabled
             {
                 weaponBoxCol.enabled = false;
             }
             else if (playerAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.5)  //If the animation is at middle of it, the collider enables
             {
                 weaponBoxCol.enabled = true;
             }
             else if (playerAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime == 0) //If the animation didn't begin, the collider stays disabled
             {
                 weaponBoxCol.enabled = false;
             }
         }
         if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Player_walk") || playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Player")) //If the animation of walking and idling are played, the collider stays disabled
         {
             weaponBoxCol.enabled = false;
         }
     }

It's maybe not optimized, but it works. The Animator.GetCurrentAnimatorStateInfo was the key!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

334 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 avatar image avatar image avatar image 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

Animator not animating a runtime instantiated child 0 Answers

Change transform of a prefab having an animation 1 Answer

GameObject variable points to instance instead of prefab when prefab gets instantiated 2 Answers

Trouble rotating a prefab while it's moving up,Rotating an object with an animation while it moves up 0 Answers

Animation controller attached to prefab instance doesn't work 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