Author Topic: Disk like Controller, Rotates with Finger.  (Read 4681 times)

Anxo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Disk like Controller, Rotates with Finger.
« on: January 19, 2013, 10:25:51 PM »
Hey Tasharen,

I am trying to create a Disk like controller that will act as a Sprite type graphic which I can spin on the screen with my finger. I looked at your Mousetospin script wich is cool but not the same.

What I am looking for is the ability to rotate slow or fast in any direction and maybe the up most point of the disk always follows my finger.

I could try to figure this out with conventional means, but as I plan to do the rest of my UI with NGUI, I would like to find a NGUI solution.

Thanks to any advice you are able to offer.

PS: a perfect reference is an IOS game called "Invading Insanity" (free) it has a controller in the lower left corner that is exactly what I am talking about.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disk like Controller, Rotates with Finger.
« Reply #1 on: January 20, 2013, 05:34:54 AM »
That's not really an NGUI question since the math to spin something is exactly the same regardless of what UI you use. It's just algebra. The easier thing to do is to set the object's rotation based on the finger's distance from where you started the drag process. Record the screen position in OnPress(true), and calculate the distance in OnDrag().

Anxo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Disk like Controller, Rotates with Finger.
« Reply #2 on: January 21, 2013, 06:32:39 PM »
Thank you for your feedback ArenMook. While it is not giving me the same result to calculate the distance of drag, I think I may have an idea on how to accomplish this task now.

This is a video of someone using the controller. http://www.youtube.com/watch?v=QVMn3NqtjMs

I love the way it follows the finger so what I think I will do is check the center position of the controller and the position of the finger and find the desired rotation angle.

I hope that will work. Thank you for taking the time to give me a hint.   ;D

Anxo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Disk like Controller, Rotates with Finger.
« Reply #3 on: January 21, 2013, 11:11:59 PM »
Ok Incase anyone else is wondering how to achieve this using the NGUI components. Here is my solution, a little ghetto but it works perfectly! ;D

First, I had to change the code in the UICamera Script so that OnDrag gets the finters position instead of the delta.
Then when you have the finger position, rotate the hole UI setup so that the camera is pointing down at the floor.

Next, I have a button and on this button I have the following script.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DiskController : MonoBehaviour {
  5.        
  6.         public Camera GuiCam;
  7.         public Transform lookatObject;
  8.        
  9.         private bool isPressed = false;
  10.  
  11.         void OnPress(bool pressing){
  12.                 Debug.Log ("pressing = "+ pressing);
  13.                 isPressed = pressing;          
  14.         }
  15.         void OnDrag(Vector2 delta){
  16.                 if(isPressed){
  17.                        
  18.                         Ray ray = GuiCam.ScreenPointToRay(delta);
  19.                         RaycastHit hit;
  20.                         if(Physics.Raycast(ray, out hit)){
  21.                                 lookatObject.transform.position = hit.point;   
  22.                         }
  23.                 }
  24.         }
  25.        
  26. }
  27.  
  28.  

All that does is position an object where ever the finger pushes while on the button. 

Then there is a game object inside of that button that while zeroed out has its local directions lined up with the global directions and on that game object I attached the following script.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RotateControlDisk : MonoBehaviour {
  5.          public Transform player;
  6.     Vector3 target;
  7.  
  8.     void Update ()
  9.     {
  10.        target = new Vector3(player.position.x, this.transform.position.y, player.position.z);
  11.         transform.LookAt(target);
  12.     }
  13. }
  14.  
  15.  
  16.  

And that works like a charm.

Cheers Unity Community!