• 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 ArthurKW · Mar 16, 2017 at 08:30 PM · scripting problemcollidersparent-child

How can i make the child collider turn off and on instead of the parent collider?

using UnityEngine;

public class ZGruntAttack : MonoBehaviour {

 public float timeBetweenAttacks = 0.5f;     // The time in seconds between each attack.
 public int attackDamage = 10;               // The amount of health taken away per attack.


 Animator anim;                              // Reference to the animator component.
 GameObject player;                          // Reference to the player GameObject.
 PlayerHealth playerHealth;                  // Reference to the player's health.
 EnemyHealth enemyHealth;                    // Reference to this enemy's health.
 bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
 float timer;                                // Timer for counting up to the next attack.
 int runHash = Animator.StringToHash("Run");
 private CapsuleCollider weaponCollider;

// UnityEngine.AI.NavMeshAgent nav;

 void Awake ()
 {
     // Setting up the references.
     player = GameObject.FindGameObjectWithTag ("Player");
     playerHealth = player.GetComponent <PlayerHealth> ();
     enemyHealth = GetComponent<EnemyHealth>();
     anim = GetComponent <Animator> ();
     weaponCollider = GetComponentInChildren<CapsuleCollider> ();
 }

 /// Start is called on the frame when a script is enabled just before
 void Start() {
     

 }


 void OnTriggerEnter (Collider other)
 {
     // If the entering collider is the player...
     if(other.gameObject == player)
     {
         // ... the player is in range.
         playerInRange = true;
     }
 }


 void OnTriggerExit (Collider other)
 {
     // If the exiting collider is the player...
     if(other.gameObject == player)
     {
         // ... the player is no longer in range.
         playerInRange = false;
         anim.SetTrigger (runHash);
     }
 }


 void Update ()
 {
     // Add the time since Update was last called to the timer.
     timer += Time.deltaTime;

     // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
     if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
     {
         // ... attack.
         Attack ();

     }


     // If the player has zero or less health...
     if(playerHealth.currentHealth <= 0)
     {
         // ... tell the animator the player is dead.
         anim.SetTrigger ("PlayerDied");
     }
 }


 void Attack () {

     // Reset the timer.
     timer = 0f;

     // If the player has health to lose...
     if(playerHealth.currentHealth > 0)
     {
         // ... damage the player.

// playerHealth.TakeDamage (attackDamage); anim.Play("Attack",-1,0f); } }

 public void EnemyBeginAttack () {
     weaponCollider.enabled = true;
 }

 public void EnemyEndAttack () {
     weaponCollider.enabled = false;
 }

}

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 rhylvin2016 · Mar 17, 2017 at 02:42 AM

dont use if(other.gameObject == player) use if(other.tag == "playerChild") and change the tag of the child of the player you want to playerChild. you do this by clicking any gameobject and look at the inspector, you'll see below is the Tag: and the Layer: click the Tag and click the Add Tag in the Tag dropdown, click the Plus sign and add the "playerChild" and click save. now you're not done yet.. you just created a new tag but you didn't apply it to the child that you want.

now find the child you want and click the tag.. and look for the playerChild tag or any tagname you want. as long as its the same in the codes if(other.tag == "playerChild")`

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Pulling boxes 0 Answers

I want to make the gameobject rigidbody.iskinematic=true for 3s and immune to damage when it triggers the collider and then false again 0 Answers

Rigidbody trapped in collider after collision 1 Answer

Collider Doesn't Rotate in Sync With Mesh 1 Answer

How to make Two colliders, don't collide, but still be able of interact with each others 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