- Home /
Help! How do we enable or disable Collider with a specific action?
Hi, I use the Third Person Controller package. And I want to activate or deactivate a series of colliders when my character is running. Please help.
Answer by KloverGames · Jul 13, 2019 at 12:58 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CollideScript : MonoBehaviour {
public MeshCollider col;
// Update is called once per frame
void Update()
{
if (Input.GetKey("w"))//You press this when the character is moving
col.enabled = true;//Making the collider active
else//when you are not pressing w
col.enabled = false;//disabling the collider
}
}
Hello greenklover. Hello. Will I add this code to the player or should I add it to Collider? Thank you for this answer
Answer by ShiNoKitsune · Jul 12, 2019 at 10:04 PM
Do a Simple Array of Colliders Like This
public bool deactive;
public Collider[] ColliderToDeatctivate;
void Update(){
if(deactive)
for(int i=0; i < ColliderToDeatctivate.Length; i++)
{
ColliderToDeatctivate[i].enabled = false;
}
}
Hi ShiNo$$anonymous$$itsune , I mean, when my character is moving, my collider is activated, and when my character stops moving, this colloder will be disabled.
public Collider col;
void Update () { if(Input.Get$$anonymous$$ey("w")) { col.enabled = true; } else col.enabled = false; }
Hold on, I know what you need. Let me open up visual studio real quick