Author Topic: Transform object with slider.  (Read 7416 times)

detomato

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Transform object with slider.
« on: February 19, 2014, 08:14:54 PM »
Hi, this probably an easy to answer which obviously not so easy for me.

I would like to use the slider to transform a game object, especially its rotation and scale. I look through if there is any integration with Playmaker, but still hitting the wall.
So I would like to know if there's easy way to do so?

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Transform object with slider.
« Reply #1 on: February 20, 2014, 10:41:37 AM »
Here's an example. Attach this script to the object you want rotated, and in the slider's On Change script choose this script's SliderChange function as the notification target.
  1. using UnityEngine;
  2.  
  3. public class MyScript : MonoBehaviour
  4. {
  5.     public void SliderChange ()
  6.     {
  7.         transform.localRotation = Quaternion.Euler(0f, 0f, 180f * UISlider.current.value));
  8.     }
  9. }

detomato

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Transform object with slider.
« Reply #2 on: February 21, 2014, 01:59:05 AM »
Here's an example. Attach this script to the object you want rotated, and in the slider's On Change script choose this script's SliderChange function as the notification target.
  1. using UnityEngine;
  2.  
  3. public class MyScript : MonoBehaviour
  4. {
  5.     public void SliderChange ()
  6.     {
  7.         transform.localRotation = Quaternion.Euler(0f, 0f, 180f * UISlider.current.value));
  8.     }
  9. }

Thanks for the script. But how does it work if I got 2 slider?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Transform object with slider.
« Reply #3 on: February 21, 2014, 03:08:50 PM »
You're the one who sets the On Change notification on the slider. If you have two sliders, choose scripts on different objects.

detomato

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Transform object with slider.
« Reply #4 on: February 22, 2014, 02:13:50 AM »
You're the one who sets the On Change notification on the slider. If you have two sliders, choose scripts on different objects.
hi, thanks for the reply. Manage to get it to work, but it only working on the Z-axis. I tried to change to 180f, 0f, 0f but nothing happen.
I know Im missing the very obvious here. Please have mercy. :)

Thanks!

edit: Its working now, not sure if its the most elegant way to do so, but at least its working.
« Last Edit: February 22, 2014, 02:36:41 AM by detomato »

detomato

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Transform object with slider.
« Reply #5 on: February 22, 2014, 02:51:30 AM »
Please forgive my ignorance, but can I get a quick help with the scaling scripts?

  1. public void SliderChange ()
  2.         {
  3.                 transform.localScale = new Vector3(2, 2, 2) * (UISlider.current.value);
  4.         }

I end up with the codes above which kinda works, but it scales to 0 when the slider is at the lowest value. I want to scale it from the current scale. If you get what I mean. :)

Thanks in advance.
« Last Edit: February 22, 2014, 03:42:35 AM by detomato »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Transform object with slider.
« Reply #6 on: February 22, 2014, 11:11:05 AM »
You seem to be asking really basic Unity-related questions here. You know how to get the slider's value -- UISlider.current.value. The rest is about how to use Unity, not NGUI. This is a support forum for NGUI, not Unity.
  1. transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 2f * UISlider.current.value);