- Home /
GUI slider and rotation not working
Hi, everyone, I'm working on an RTS, and I want the player to be able to edit the rotation of the buildings that he got. My building works like this: [Parent Object, with the appropriate name] [model w/ mesh collider] [any other gameobjects for the building]
This script is attatched to the building:
enum TypeOfBuilding { House, Forge, SoldireHouse, Castle, OtherObject } var typeOfBuilding : TypeOfBuilding; var health : int; var Name : String; var selectedColor : Color; class SoldireHouseVars { var creationRate : float; var Soldire : GameObject; var creationPoint : GameObject; } var SoldireHouseVars : SoldireHouseVars = SoldireHouseVars;
@HideInInspector var hSliderValue : float = 0.0;
private var showBox : boolean = false; private var showStats : boolean = false; private var lastCreation : float;
function Update() { if(typeOfBuilding == TypeOfBuilding.SoldireHouse) { if(Time.time > lastCreation) { lastCreation = Time.time + SoldireHouseVars.creationRate; var go : GameObject = Instantiate(SoldireHouseVars.Soldire, SoldireHouseVars.creationPoint.transform.position, transform.rotation); go.AddComponent(Soldire); } } }
function OnMouseOver () { if(showBox != true) { showBox = true; } if(Input.GetMouseButtonDown(0)) { showStats = true; } } function OnMouseExit () { if(showBox == true) { showBox = false; } } function OnGUI() { if(showBox) { GUI.Label(Rect(5, 5, 200, 30), typeOfBuilding.ToString()); } if(showStats) { Time.timeScale = 0; GUI.Box(Rect(Screen.width-150, 0, 150, Screen.height), Name); if(GUI.Button(Rect(Screen.width-150, Screen.height-40, 150, 20), "Delete")) { Destroy(gameObject); Time.timeScale = 1; } if(GUI.Button(Rect(Screen.width-150, Screen.height-20, 150, 20), "Done")) { Time.timeScale = 1; showStats = false; transform.rotation.y = hSliderValue; } hSliderValue = GUI.HorizontalSlider (Rect (Screen.width-140, 30, 135, 30), hSliderValue, 0.0, 360.0); } } function GetName() { return(Name); }
The GUI slider is my problem. When I set it, unless it is up against the very edge (at 0), it is 180 degrees. I want it to be at the value of the slider. How would I do this?
Thanks for the help!!! :)
Transform.rotation is a Quaternion...a non-intuitive 4D construct. You don't what to address the components (x,y,z,w) directly. With care you can modify eulerAngles.
Answer by gazza529 · May 30, 2013 at 02:52 PM
hopefully this will help you as i had the same problem
var hSliderValue : float = 0.0;
var target : Transform;
function OnGUI ()
{
hSliderValue = GUI.HorizontalSlider (Rect (400, 200, 100, 30), hSliderValue, 0.0, 360.0);
}
function Update ()
{
target.transform.rotation.eulerAngles.y = hSliderValue;
}
You should not set eulerAngles independently. From the reference:
Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.
You could do this ins$$anonymous$$d (assu$$anonymous$$g the other two rotation are 0):
target.transform.rotation.eulerAngles = Vector3(0.0, hSliderValue, 0.0);