- Home /
 
Found a workaround to my issue.
How to set a Collider of a GameObject to Inactive similar to the SetActiveMethod
I am curious as to how I could set a component/collider to inactive and active again after a period of time has passed. I'm trying to do this to make up for the hindsight I had for an animation I had created for many characters where they jump but the collider would stay stay still and run into things that would cause damage to the player.
Here is the code I am working on:
 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour 
 {
 
     public float movementSpeed = 10.0f;
     public float angleChangeSpeed = 100.0f;
 
     public bool isDead = false;
     
     private float minX = -15.0f;
     private float maxX = 15.0f;
     private float minZ = 0.0f;
     private float maxZ = 20.0f;
 
     private GameObject playerCharacter;
 
     void Start()
     {
         playerCharacter = GameObject.FindGameObjectWithTag ("PlayerCharacter");
     }
 
     // Update is called once per frame
     void Update () 
     {
         if(!isDead)
         {
             transform.localPosition = new Vector3(Mathf.Clamp(transform.localPosition.x, minX, maxX), 0, 
                                              Mathf.Clamp(transform.localPosition.z, minZ, maxZ));
             // new code
             float horizontal = Input.GetAxis ("Horizontal");
             float vertical = Input.GetAxis ("Vertical");
 
             Vector3 direction = new Vector3 (horizontal, 0, vertical);
             Vector3 finalDirection = new Vector3 (horizontal, 0, 1.0f);
 
 
             transform.position += direction * movementSpeed * Time.deltaTime;
 
             transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (finalDirection), Mathf.Deg2Rad*angleChangeSpeed);
         }
 
 
         if (!isDead) 
         {
             //THIS BODY OF CODE WILL TRY TO SET COLLIDER INACTIVE
             if (Input.GetButton ("Jump"))
             {
                 playerCharacter = GameObject.GetComponent<Collider>.SetActive(false);
                 //I know this code doesn't work, it is pseudocode to get my point across
             }
         }
     }
 
     void SetDead(bool dead)
     {
         Debug.Log ("SetDead was reached");
         isDead = dead;
     }
 
     void ResetPosition()
     {
         transform.localPosition = Vector3.zero;
     }
 }
 
               If anyone could give me advice on this matter that would be awesome and thank you.
Answer by wesleywh · Jan 23, 2015 at 09:17 PM
Hum... So if I understand you correctly you just want to try to turn off your Collider? You could do this with:
 this.GetComponent<SphereCollider>().enabled = false; //This could work for almost any component
 
               So when your not on the ground you could run this code.
Thank you I didn't know how to phrase the code and needed help. Also the game object in question that I wanted to have its capsule collider to be inactive is the child of the game object that this script is used on, if that makes sense. I didn't think that the parent child relationship would matter sorry.
Update: I found out how to make so that children would be affected specifically, it was just this.GetComponentInChildren<>().enable=false
now I've looked into yield and wait but I'm not sure which one to use so that I can reactivate the collider that I wanted gone so the player can receive damage again. Seriously thanks for the help so far but sorry I need this clarification for my $$anonymous$$d's sake. UPDATE: I've found a method called invoke and I've messed around with it to make it do what I want. thanks again for helping me.
NNNooooo problem. Glad you were able to find all of that out.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Arongranbergs Astar DynamicGridObstacle deletes nodes from gridgraph 0 Answers
Trouble with Inaccurate Mesh Collider 0 Answers
C# Assigning EdgeCollider2D's Points 0 Answers