- Home /
Rotating Gameobject with (hidden) UI slider and Inputfield both positive and negativ
Hi there,
I got a new issue regarding my project, this time it's split in different points:
I need to rotate a gameobject by a slider or slidefunction and inputfield around one axis. Means if i use the slider, the value on the inputfield needs to change accordingly.
I also just want to see a button instead of a whole slider, so basically I'd need to press the button and then change the rotation by sliding left and right. Currently I just positioned the slider above the button which is not intereactable and set the alpha to 0, so it's invisible, but maybe ther is another possibility to achieve this?
The other (and bigger issue) is, that I need the correct rotation in both positive and negative values but currently the object rotates just from 0° - 360° (I know that basically, this is right,as it transfers the negative angles from the negative space to a value from 0°-360°, but I NEED the values as put in in the inputfield or set by the slider for evaluation). Any idea how to manage this?
Thank you in advance!
best regards
Answer by stevecus · Jan 26, 2015 at 04:17 PM
Just assign a slider max value and min value. As for just wanting the button, create a gameobject>UI>Slider, on the slider delete the image component and delete the child fill area. You can tick use whole numbers on the slider if you don't want floats.
Here,
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SliderRotate : MonoBehaviour {
public Slider slider;
public float maxRotatePos;
public float maxRotateNeg;
public float currentRotation;
void Awake()
{
//assign slider attributes
if(maxRotatePos == 0)
{
maxRotatePos = 360;
}
if(maxRotateNeg == 0)
{
maxRotateNeg = -360;
}
slider.maxValue = maxRotatePos;
slider.minValue = maxRotateNeg;
currentRotation = 0;
}
// Update is called once per frame
void Update ()
{
currentRotation = slider.value;
transform.eulerAngles = new Vector3(0, currentRotation, 0);
}
}
Steve.
ok, thank you for that! :) Any idea how to manage the inputfield issue? To link the values of the slider and the inputfield correctly?
Sorry, I don't quite understand, in the inspector you should have the public float of currentRotation. Whatever this value is is what the slider value is and thus you can use this variable to reference the rotation for other scripts. Is that what you want?
Answer by CodingNoob · Jan 27, 2015 at 03:21 PM
Yes, and your script works well! I just need to be able to change the rotation of the object both with the slider or the inputfield. I linked the slider.value to the input.text, so if the slider.value changes it is shown on the inputfield. But if I want to type the rotation into the textfield, it doesn't work out as I'd like it to.
I set the input.text to set slider.value if changed (slider.value = float.Parse(input.text), and set the Inputfield inspector to accept Decimal Numbers only. But it's having trouble when I try to type in a negative value... I guess there is an issue with converting the string of the Inputfield to a float, is there any other option for this? Also the Placeholder shows up everytime the Textfield is empty (even while still editing the text) and I can't figure out how to reset the value of the Textfield as soon as I klick the Textfield (or: start Editing)...
Ahh I understand what you want now, apologies. Ill have a look tonight and see what can be done.
I managed the issue by using a bool! :D
for the inputfield I added a Event Trigger and the Pointer Click event. By clicking (entering) the inputfield, the bool is set to false and by exiting (EndEdit) to true. The default state of the bool is true. Now, if the bool is false, the Update stops checking for the
input.text = slider.value.ToString(); slider.value = float.Parse(input.text);
part, which was causing the issue as it seems.
Only issue left is reseting the inputfield to an empty value when entering...
Can you tell us how you're fix solution looks like to make the Input field! Thank you!