Search Unity

MouseOverMesh problem....

Discussion in 'Scripting' started by Konstantinos, Dec 11, 2007.

  1. Konstantinos

    Konstantinos

    Joined:
    Jan 12, 2007
    Posts:
    31
    Hello everyone,

    I m trying to make a turning turntable with one box on it. When the user hovers the mouse
    over the box then the box will become red. (exactly like the MouseOverMesh example on the
    unity3d example projects scripts example)

    The problem is that the MouseOverMesh script fails when i parent the box to the turntable....
    I searched the forums but could not resolve this..
    i'd appreciate any help.

    Here's what i do...

    1) create a cube; make it Rigidbody; disable gravity; scale it on x and z; name it TurnTable

    2)create a cube; make it Rigidbody;set is kinematic; disable gravity; move it just under the TurnTable; name it anchor

    3) attach a hinge Joint to the anchor; set as "connected body" the TurnTable and set Y only as rotation axis.

    4) Attach a "Drag Rigidbody" script to the turntable

    Now we have a "Turntable" that the user can drag-n-rotate with the mouse.

    we place a small box on the TurnTable

    5) create a cube name it box;

    6) attach a "MouseOverMesh" script to change the box's color when we put the mouse over it.

    (MouseOverMesh script can be found on the unity site http://unity3d.com/support/documentation/examples/ScriptExamples.zip)

    we can see that when the mouse is over the box, the box becomes red. (good so far)

    ok we now want the box to rotate along with the TurnTable

    7) we parent the box under the TurnTable ...


    and the MouseOverMesh script stops working :( .... the box does not change color anymore when the mouse is over the box.

    any ideas or workarounds?


    Thanks in advance

    Konstantinos




    Ps. i attach the MouseOverMesh script here, so you don't have to search around.

    MouseOverMesh.js
    ----------------

    /// This script changes the material color to the highlightColor when the
    /// mouse hovers over a mesh or text. The highlightColor can be modified in the
    /// inspector. This script scales up the mesh or text when clicking on it.

    // All public variables are visible in the inspector and can be edited there.
    var highlightColor = Color.red;

    function OnMouseEnter () {
    // Change material color to highlight color
    renderer.material.color = highlightColor;
    }

    function OnMouseExit () {
    // Change material color back
    renderer.material.color = Color.white;
    }

    function OnMouseDown () {
    // Scale up the text
    transform.localScale = transform.localScale * 1.5;
    }

    function OnMouseUp () {
    // Scale down the text to the original value
    transform.localScale = transform.localScale / 1.5;
    }
     

    Attached Files:

  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The problem is that when you put objects together as parents/children, then separate colliders become a single, compound collider. The simplest solution: don't parent the box to the turntable. Just give it a rigidbody with useGravity = on, and it will move with the turntable, especially with high friction values.

    The downside, which maybe you're trying to avoid, is that enough torque on the turntable will cause the box to slip/fall off. In this case, you could use a dummy object. Make a box with no collider, and parent it to the turntable, then turn the renderer off. Then use this script on the "real" box, which remains a separate object:

    Code (csharp):
    1. var dummyObject : Transform;
    2.  
    3. function LateUpdate () {
    4.     transform.position = dummyObject.position;
    5.     transform.rotation = dummyObject.rotation;
    6. }
    --Eric
     
  3. Konstantinos

    Konstantinos

    Joined:
    Jan 12, 2007
    Posts:
    31
    Exactly what i'm trying to avoid. If this was not enough, the parented objects obay to the DragRigidbody script (which was originaly applied only to the Turntable) and you can literally pick them up and throw them around ... :)

    I would like to thank you because your "dummyobject" solution works like a charm...

    My objects are rotating, they stay put on the TurnTable, and they get highlithted properly :)

    Thanks again,

    Konstantinos

    Ps ..i attach a screenshot of my "actual" TurnTable
     

    Attached Files:

  4. 64746c

    64746c

    Joined:
    Oct 21, 2007
    Posts:
    181
    (Slightly OT)
    I took a look at the MouseOverMesh script and noticed a potential problem: the object's color would be returned to Color.white, even if the original color was different. I fixed this script by getting the original material color in OnMouseOver and returning to that in OnMouseExit. Here's the (complete) script:
    Code (csharp):
    1.  
    2. /// This script changes the material color to the highlightColor when the
    3. /// mouse hovers over a mesh or text. The highlightColor can be modified in the
    4. /// inspector. This script scales up the mesh or text when clicking on it.
    5.  
    6. // All public variables are visible in the inspector and can be edited there.
    7. var highlightColor = Color.red;
    8.  
    9. // originalColor is to keep track of the material's original color, in case
    10. // it was something other than white.
    11. private var originalColor : Color;
    12.    
    13. function OnMouseEnter () {
    14.     // get original material color
    15.     originalColor = renderer.material.color;
    16.     // Change material color to highlight color
    17.     renderer.material.color = highlightColor;
    18. }
    19.  
    20. function OnMouseExit () {
    21.     // Change material color back
    22.     // Modified to take varying original colors into account
    23.     renderer.material.color = originalColor;
    24. }
    25.  
    26. function OnMouseDown () {
    27.     // Scale up the text
    28.     transform.localScale = transform.localScale * 1.5;
    29. }  
    30.  
    31. function OnMouseUp () {
    32.     // Scale down the text to the original value
    33.     transform.localScale = transform.localScale / 1.5;
    34. }
     
  5. Konstantinos

    Konstantinos

    Joined:
    Jan 12, 2007
    Posts:
    31
    Help much appreciated DannyL....

    Thank you for your useful tweak.


    Konstantinos