Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sliders (Tilt and Yaw Rotation)

Discussion in 'Scripting' started by sneakybig, Jun 12, 2018.

  1. sneakybig

    sneakybig

    Joined:
    May 25, 2018
    Posts:
    3
    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:
    Code (CSharp):
    1.  using System.Collections;
    2.       using System.Collections.Generic;
    3.       using UnityEngine;
    4.       using UnityEngine.UI;
    5.      
    6.       public class ObjectControl : MonoBehaviour {
    7.      
    8.           public GameObject objectToRotate;
    9.           public Slider rotateSlider;
    10.           public Slider tiltSlider;
    11.           public Slider scaleSlider;
    12.      
    13.      
    14.           public void RotateMyObject(float sliderValueR)
    15.           {
    16.               sliderValueR = rotateSlider.value;
    17.               objectToRotate.transform.rotation = Quaternion.Euler(0, sliderValueR * 360, 0);
    18.           }
    19.      
    20.      
    21.           public void TiltMyObject(float sliderValueT)
    22.           {
    23.               sliderValueT = tiltSlider.value;
    24.               objectToRotate.transform.rotation = Quaternion.Euler(0, 0, sliderValueT * 360);
    25.           }
    26.      
    27.           public void ScaleMyObject(float sliderValueS)
    28.           {
    29.               Vector3 scale = objectToRotate.transform.localScale;
    30.               sliderValueS = scaleSlider.value;
    31.      
    32.      
    33.               objectToRotate.transform.localScale = new Vector3(sliderValueS, sliderValueS, sliderValueS);
    34.      
    35.           }
    36.      
    37.       }
    38.  
    Thank you all for the help!!
    Fabio
     
  2. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    I see where the problem, i can modify your code, but first i need one thing, how you are calling RotateMyObject and TiltMyObject, from where? And why you need this (float sliderValueR) in your voids ?
     
  3. sneakybig

    sneakybig

    Joined:
    May 25, 2018
    Posts:
    3
    THANK YOU Bakir for stepping in! Im using Unity 3d to create a very simple Augmented Reality Scene. Basically is one object in the middle that currently is being scaled, tilted and rotated using three different sliders:
    Screen Shot 2018-06-12 at 10.36.14 AM.png

    Each slider is calling RotateMyObject, TiltMyObject, ScaleMyObject:
    Screen Shot 2018-06-12 at 10.36.07 AM.png

    Right now, they are all working fine independently, so, when I rotate, it works fine, but then when I tilt, it goes back to the original position and then tilts, instead of storing the current position and tilting from there on.

    Please let me know if you have more questions and thank you once again!
     
  4. bakir-omarov

    bakir-omarov

    Joined:
    Aug 1, 2015
    Posts:
    48
    I already answered your question in Unity Answers: https://answers.unity.com/questions/1512173/sliders-tilt-and-yaw-rotation-1.html

    The problem was:
    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.

    I added new code on Unity Answers. Hope it will help.
     
  5. sneakybig

    sneakybig

    Joined:
    May 25, 2018
    Posts:
    3
    Hi Bakir, thank you so much! 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:
    I found that this is a very common problem just not sure how this can be fixed in my script... Thank you again for your time!