Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jan 24, 2015 at 03:00 AM by Ujean917 for the following reason:

Found a workaround to my issue.

avatar image
0
Question by Ujean917 · Jan 23, 2015 at 07:00 PM · c#collidersetactive

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.

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

  • Sort: 
avatar image
6
Best Answer

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.

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 Ujean917 · Jan 24, 2015 at 12:43 AM 0
Share

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.

avatar image wesleywh · Jan 24, 2015 at 05:50 AM 1
Share

NNNooooo problem. Glad you were able to find all of that out.

Follow this Question

Answers Answers and Comments

20 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

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


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