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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rigid body, am I missing something here?

Discussion in 'Editor & General Support' started by stevemachineHK, Mar 20, 2012.

  1. stevemachineHK

    stevemachineHK

    Joined:
    May 6, 2010
    Posts:
    18
    Having great trouble here, I have a train cab model with various interactive buttons and switches (on mouse over events and raycast hit events).

    The problems comes when I attach a rigid body to the cab all the interactive buttons inside the cab suddenly stop working.

    Cant find anything in the the forums or the reference about this, am I missing something here?
     
  2. yuzu_drink

    yuzu_drink

    Joined:
    Jan 15, 2012
    Posts:
    14
    Is the hit being detected first by the cab's collision box? Does your hit test return ANY colliders or does it return false?

    You might try Physics.RaycastAll (http://unity3d.com/support/documentation/ScriptReference/Physics.RaycastAll.html) and see if that returns any answers about what you're hitting.

    Also, it doesn't sound like this is the case, but the docs mentioned something about Physics.Raycast returning false when called from within a sphere? So just check to make sure that's not your issue either.

    Otherwise, try the raycast all and see what all you're colliding with. You might have to add an extra layerfor JUST the buttons and use a layer filter to ONLY raycast with colliders labeled as that layer. Probably the most efficient way to pull this one off anyway.

    Let me know if any of this ends up being useful. :)
     
  3. stevemachineHK

    stevemachineHK

    Joined:
    May 6, 2010
    Posts:
    18
    Ill try that, thanks but Its not only the Raycast thats the problem, my OnMouseOver functions stop working once i attach a rigidbody to the GameObject, any ideas why that should be happening?
     
  4. stevemachineHK

    stevemachineHK

    Joined:
    May 6, 2010
    Posts:
    18
    heres my mouse over script

    var originalColor;
    var highlightMultiply = 4;
    var me;
    var cursor : Texture2D;
    var link : Texture2D;
    var normal : Texture2D;


    function Start() {
    originalColor = renderer.material.color;
    me = GameObject.FindWithTag("Player");
    }

    function OnMouseOver() {
    var dist = Vector3.Distance(me.transform.position, transform.position);
    if(dist < 5){
    renderer.material.color.g = originalColor.g*highlightMultiply;
    renderer.material.color.b = originalColor.b*highlightMultiply;
    renderer.material.color.r = originalColor.r*highlightMultiply;
    Screen.showCursor = false;
    cursor = link;
    }
    }

    function OnMouseExit() {
    renderer.material.color = originalColor;
    Screen.showCursor = true;
    cursor = normal;
    }
     
  5. stevemachineHK

    stevemachineHK

    Joined:
    May 6, 2010
    Posts:
    18
    and heres my raycast script, both work fine untill I attach a rigidbody component

    var cabinLight:GameObject;

    var lightOn=true;
    function Update ()
    { var hit:RaycastHit;
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);


    if(Input.GetMouseButtonDown(0))
    {
    if(Physics.Raycast( ray, hit, 3 )&lightOn==true)
    switch(hit.transform.tag)
    {

    case "CabLight":


    cabinLight.light.range=0;
    lightOn=false;


    }
    else if(Physics.Raycast( ray, hit, 3 )&lightOn==false)

    {

    cabinLight.light.range=4;
    lightOn=true;

    }
    }
    }