Sliders (Tilt and Yaw Rotation)
Sliders (Tilt and Yaw Rotation) Hi guys, Im new to unity and I have been trying to create slider controllers to rotate my object... So I created two sliders, one for tilt (Z) and yaw (Y), Both works great on their own, my problem is when I try to use them together. If I move the Yaw half way and then I move the Tilt, the object returns to its original position instead of storing the current position and adjusting from then on. Hope this makes sense... I know my main problem is that I have no clue how to store the rotation coordinates or how to use them to make my sliders work correctly. Here is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ObjectControl : MonoBehaviour {
public GameObject objectToRotate;
public Slider rotateSlider;
public Slider tiltSlider;
public Slider scaleSlider;
public void RotateMyObject(float sliderValueR)
{
sliderValueR = rotateSlider.value;
objectToRotate.transform.rotation = Quaternion.Euler(0, sliderValueR * 360, 0);
}
public void TiltMyObject(float sliderValueT)
{
sliderValueT = tiltSlider.value;
objectToRotate.transform.rotation = Quaternion.Euler(0, 0, sliderValueT * 360);
}
public void ScaleMyObject(float sliderValueS)
{
Vector3 scale = objectToRotate.transform.localScale;
sliderValueS = scaleSlider.value;
objectToRotate.transform.localScale = new Vector3(sliderValueS, sliderValueS, sliderValueS);
}
}
Thank you all for the help!! Fabio
Answer by bakir-omarov · Jun 12, 2018 at 11:33 AM
Hey! When you are rotating one axis, you are returning to zero another two, see : Quaternion.Euler(0, 0, sliderValueT * 360). You just need to use current axis rotations. See on the code ;)
Here is the code:
public void RotateMyObject(float sliderValueR) // i don't know why you are need this |float sliderValueR| here?
{
sliderValueR = rotateSlider.value;
Vector3 currentRotation = objectToRotate.transform.rotation.eulerAngles;
objectToRotate.transform.rotation = Quaternion.Euler(currentRotation.x, sliderValueR * 360, currentRotation.z);
}
public void TiltMyObject(float sliderValueT) // i don't know why you are need this too
{
sliderValueT = tiltSlider.value;
Vector3 currentRotation = objectToRotate.transform.rotation.eulerAngles;
objectToRotate.transform.rotation = Quaternion.Euler(currentRotation.x, currentRotation.y, sliderValueT * 360);
}
P.S i tested script, rotate it million times, no glithces no bugs. I am using Unity 2017.
Wow! you are amazing! It is so easy to get stuck when you don't know all the available options when coding...
If you don't $$anonymous$$d me asking, as the beginner I am, what is the best source to get better at this? $$anonymous$$ost of my program$$anonymous$$g experience has been in ExtendScript to create user interfaces for After Effects of when I was learning about that, there were good video courses to follow along... Hope to find the same now that I am getting more into Unity.
Thank you once again!!!
Fabio
For the beginning my advice is to follow 5-10 full courses (tutorial series). For example you can find a bunch of them here: https://unity3d.com/learn/tutorials/
Or just type in youtube "how to make unity game", and watch all video of some playlist from episode 1 till the end, and parallel implement all this stuff in your project. Have a nice Unity trip !
Hi Bakir, let me ask you another question, after making the changes, the rotation and the tilt are working well together, but for some reason, when I tilt, I get very strange results, as I move the slider, at certain amount of degrees, the object starts to flicker. I recorded the problem for you to see: https://vimeo.com/274754621 I found that this is a very common problem and found this might be a solution: https://answers.unity.com/questions/395033/quaternionfromtorotation-misunderstanding.html Just not sure how this applies to my script in order to fix it... Thank you again for your time! Fabio
Your answer
Follow this Question
Related Questions
How can I move Object over local axis with sliders 1 Answer
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
how to shoot on different direction while moving away 1 Answer
How can I properly tilt an object about two axes, while preventing the other axis from rotating? 1 Answer
How to keep the same yaw(right/left) and pitch(up/down) controls rotation as I roll ? 0 Answers