okay, so oddly, I was writing a script this evening that covers your very problem at hand…
I thought it may help people to understand game object actions with the mouse a bit better.
It also incorporates an enum to allow you to change the rotations through a convenient UI on the left.
The OnMouse methods basically are called every frame automatically at runtime so are just plugged into like Update.
Below is the script to take
using UnityEngine;
using System.Collections;
public class Platform : MonoBehaviour
{
public GameObject platform; //gameobject to contrl rotations on
public bool moving = false; //script boolean
public float rotationSpeed = 20.0f; //multiplier
public float[] variants; //can be used to vary (inspector only - not in GUI buttons)
//inspector accessed enumerator type
public enum AxisRotationChoice { AntiClockWiseY, ClockWiseY, AntiClockWiseX, ClockWiseX, AntiClockWiseZ, ClockWiseZ, AntiClockWiseMix, ClockWiseMix };
//script based cache to operate on
public AxisRotationChoice axisRotationChoice; //changeable through GUI and Inpector
private AxisRotationChoice axisRotChoice;
#region StopRotation
/// <summary>
/// this is called to stop the platform rotating by changing the script controlling boolean to false
/// </summary>
void StopRotation()
{
Debug.Log("
message received");
moving = false;
Debug.Log("
moving is now…: " + moving);
}
#endregion
#region RotatePlatform
///
/// Calls ChangeRotation and perform any necessary chage to the rotational axis
/// selected through GUI buttons on screen.
///
void RotatePlatform()
{
ChangeRotation();
Debug.Log("
message received");
moving = true;
Debug.Log("
moving is now…: " + moving);
}
#endregion
#region OnGUI
///
/// Delivers UI functionality to platform.
/// A button can be selected and when the OnMouseOver event is fired,
/// it in turn fires off the RotatePlatform method, which checks for any changes.
///
///
void OnGUI()
{
GUI.BeginGroup(new Rect(0.0f, 10.0f, 500.0f, 500.0f));
if (GUI.Button(new Rect(10.0f, 0.0f, 100.0f, 25.0f), “ClockWiseY”))
{
axisRotationChoice = AxisRotationChoice.ClockWiseY;
}
if (GUI.Button(new Rect(10.0f, 27.0f, 100.0f, 25.0f), “ClockWiseZ”))
{
axisRotationChoice = AxisRotationChoice.ClockWiseZ;
}
if (GUI.Button(new Rect(10.0f, 54.0f, 100.0f, 25.0f), “ClockWiseMix”))
{
axisRotationChoice = AxisRotationChoice.ClockWiseMix;
}
if (GUI.Button(new Rect(10.0f, 81.0f, 100.0f, 25.0f), “AntiClockWiseX”))
{
axisRotationChoice = AxisRotationChoice.AntiClockWiseX;
}
if (GUI.Button(new Rect(10.0f, 108.0f, 100.0f, 25.0f), “AntiClockWiseY”))
{
axisRotationChoice = AxisRotationChoice.AntiClockWiseY;
}
if (GUI.Button(new Rect(10.0f, 135.0f, 100.0f, 25.0f), “AntiClockWiseZ”))
{
axisRotationChoice = AxisRotationChoice.AntiClockWiseZ;
}
if (GUI.Button(new Rect(10.0f, 162.0f, 100.0f, 25.0f), “AntiClockWiseMix”))
{
axisRotationChoice = AxisRotationChoice.AntiClockWiseMix;
}
if (GUI.Button(new Rect(10.0f, 189.0f, 100.0f, 25.0f), “ClockWiseX”))
{
axisRotationChoice = AxisRotationChoice.ClockWiseX;
}
GUI.EndGroup();
}
#endregion
#region ChangeRotation
///
/// Switches through one of a set of possible enum outcomes (specified through the GUI button selected on screen)
/// and applys that to current rotational axis of the platform
///
void ChangeRotation()
{
axisRotChoice = axisRotationChoice;
switch (axisRotChoice) /// enumerator action based on inspector selected choice - changes are updated at runtime too for editor debugging
{
case AxisRotationChoice.AntiClockWiseY
:
transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed); /// rotate anticlockwise on Y
break;
case AxisRotationChoice.ClockWiseY
:
transform.Rotate(Vector3.down * Time.deltaTime * rotationSpeed); /// rotate clockwise on Y
break;
case AxisRotationChoice.AntiClockWiseX
:
transform.Rotate(Vector3.left * Time.deltaTime * rotationSpeed); /// rotate anticlockwise on X
break;
case AxisRotationChoice.ClockWiseX
:
transform.Rotate(Vector3.right * Time.deltaTime * rotationSpeed); /// rotate clockwise on X
break;
case AxisRotationChoice.AntiClockWiseZ
:
transform.Rotate(Vector3.forward * Time.deltaTime * rotationSpeed); /// rotate naticlockwise on Z
break;
case AxisRotationChoice.ClockWiseZ
:
transform.Rotate(Vector3.back * Time.deltaTime * rotationSpeed); /// rotate clockwise on Z
break;
case AxisRotationChoice.AntiClockWiseMix
:
transform.Rotate(Time.deltaTime * rotationSpeed, Time.deltaTime * rotationSpeed, Time.deltaTime * rotationSpeed); /// rotate naticlockwise on Z
break;
case AxisRotationChoice.ClockWiseMix
:
transform.Rotate(Time.deltaTime * -rotationSpeed, Time.deltaTime * -(rotationSpeed - 10), Time.deltaTime * -(rotationSpeed + 10)); /// rotate anticlockwise on Z
/// Some hardcoding has been applied in the parameter`s arguments below just to vary the rotation of each axis, as they all spin together simultaneously
break;
default:/// Here we could default to a rotation by removing a case option from above and just adding the functional bit...
/// "transform.Rotate(of your choice)" into this section
/// and removing/ commenting out this operation below.
/// Do not rotate as a default - never achieved but a safe ending and good practice - though h
transform.Rotate(0.0f, 0.0f, 0.0f);
break;
}
}
#endregion
#region Update
/// <summary>
/// Checks the boolean and fires RotatePlatform() if true
/// StopPlatform() if false
/// </summary>
void Update()
{
if (moving)
{
RotatePlatform();
}
if (!moving)
{
StopRotation();
}
}
#endregion
#region OnMouseOver
/// <summary>
/// uses the OnMouseExit event to fire if the cursor has left the object containing this script
/// Unity auto fires this script every frame or something as long as it is attached to a collider based object
/// </summary>
void OnMouseOver()
{
/// cache the public enum to the private enum instance for safe operations
//if(Input.GetMouseButton(0))
//{
RotatePlatform();
//}
}
#endregion
#region OnMouseExit
/// <summary>
/// uses the OnMOuseOver event to fire if the cursor is over the object containing this script
/// Unity auto fires this script every frame or something as long as it is attached to a collider based object
/// </summary>
void OnMouseExit()
{
StopRotation();
}
#endregion
}
IMPORTANT INSTRUCTIONS FOR USE
You should make a NEW scene and place an new gameobject( i.e. a cube) in front of your camera viewport.
Place the above script on that newly created gameobject.
Drag your newly created gameobject over to the script in the Inspector Panel, from the hierarchy panel.
Press play and mouse over the game object in your scene alongside clicking a button to the left to change rotations before placing your mouse over the object again.
Edit:
Here is a packaged [24216-mouseoverlesson.zip|24216] that should just work , add in to your project and play the scene included.
Cheers dude.
Hope that helps bud.
Take care
Gruffy