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; }
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): var dummyObject : Transform; function LateUpdate () { transform.position = dummyObject.position; transform.rotation = dummyObject.rotation; } --Eric
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
(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): /// 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; // originalColor is to keep track of the material's original color, in case // it was something other than white. private var originalColor : Color; function OnMouseEnter () { // get original material color originalColor = renderer.material.color; // Change material color to highlight color renderer.material.color = highlightColor; } function OnMouseExit () { // Change material color back // Modified to take varying original colors into account renderer.material.color = originalColor; } 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; }