Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Turning off collider in script C#

Discussion in 'Scripting' started by Thingymabob, May 6, 2014.

  1. Thingymabob

    Thingymabob

    Joined:
    Dec 10, 2013
    Posts:
    11
    So pretty much the idea is that I do not want the collider on my given object active unless I actually need it to be. In this instance I want it off normally but to turn on when I activate my raycast to instantiate an object onto the surface. Now I haven't tried this sort of thing before and while I found a few things they don't seem to want to play nice. Not sure where the issue is and I was wondering if anyone might be able to steer me in the right direction.

    Code (csharp):
    1. public class Raycast : MonoBehaviour
    2. {
    3.  
    4.     public Transform Cheese;
    5.     public Transform Sphere;
    6.     public Transform Box;
    7.     public Transform Capsule;
    8.     public Transform Floor;
    9.  
    10.     void OnGUI()
    11.     {
    12.         if(GUI.Button(new Rect(10,10,150,100), "I make Cheese"))
    13.         {
    14.             Cheese = Box;
    15.         }
    16.         if(GUI.Button(new Rect(10,100,150,100), "I make sphere's"))
    17.         {
    18.             Cheese = Sphere;
    19.         }
    20.         if( GUI.Button(new Rect(10,200, 150,100), "I make Capsules'"))
    21.         {
    22.             Cheese = Capsule;
    23.         }
    24.        
    25.     }
    26.  
    27.     void Update ()
    28.     {
    29.  
    30.         Ray ray;
    31.         RaycastHit hit;
    32.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    33.         Debug.DrawRay(ray.origin, ray.direction * 100, Color.cyan);
    34.         if(Input.GetButtonDown("Fire1"))
    35.         {
    36.  
    37.             Floor.gameObject.collider.enabled = true;
    38.            
    39.                 if (Physics.Raycast(ray, out hit, 100.0f))
    40.                 {
    41.  
    42.                 if (hit.collider.tag=="Floor")
    43.                     Instantiate(Cheese, hit.point, Quaternion.identity);
    44.                     Floor.gameObject.collider.enabled = false;
    45.                 }
    46.         }
    47.  
    48.     }
    49. }
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Hello!

    I believe that even if you are turning on and off the collider, the physics may not actually be updated until the next FixedUpdate() cycle.

    I would suggest putting such GameObjects with colliders on their own layer. You can use PhysicsSettings to make everything else in the game ignore those colliders and make your Raycasts *only* hit such colliders.
     
  3. Thingymabob

    Thingymabob

    Joined:
    Dec 10, 2013
    Posts:
    11
    Thanks, i'll have to take a look into that.