Author Topic: NGUI: Draggable Camera and Scroll Panel  (Read 3529 times)

Der_Kevin

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
NGUI: Draggable Camera and Scroll Panel
« on: April 15, 2014, 08:05:25 AM »
Hey Guys!

Iam having a problem...
I wanted to create an RTS kind of Game so i need a Draggable Camera. For the Troop Selection i wanted to use a Scrollable List.

now my biggest problem is, that when i use the scroll list, the whole background is scrolling. i think you can see the issue pretty clear when you look at this example:

http://pixelpizza.de/unity/nguidrag.html

does anybody know how i can solve this issue?

the code for the scroll view is the following:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ViewDrag : MonoBehaviour {
  5.     Vector3 hit_position = Vector3.zero;
  6.     Vector3 current_position = Vector3.zero;
  7.     Vector3 camera_position = Vector3.zero;
  8.     float z = 0.0f;
  9.  
  10.     // Use this for initialization
  11.     void Start () {
  12.  
  13.     }
  14.  
  15.     void Update(){
  16.        if(Input.GetMouseButtonDown(0)){
  17.          hit_position = Input.mousePosition;
  18.          camera_position = transform.position;
  19.  
  20.        }
  21.        if(Input.GetMouseButton(0)){
  22.          current_position = Input.mousePosition;
  23.          LeftMouseDrag();        
  24.        }
  25.     }
  26.  
  27.     void LeftMouseDrag(){
  28.        current_position.z = hit_position.z = camera_position.y;
  29.  
  30.        Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
  31.  
  32.        // Invert direction to that terrain appears to move with the mouse.
  33.        direction = direction * -1;
  34.  
  35.        Vector3 position = camera_position + direction;
  36.  
  37.        transform.position = position;
  38.     }
  39. }
  40.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: Draggable Camera and Scroll Panel
« Reply #1 on: April 15, 2014, 09:46:57 AM »
Your problem lies in the fact that you are simply using:
Quote
Input.GetMouseButtonDown(0))
...without bothering to check to see if NGUI is using this event. You're going to Unity directly, instead of to NGUI.

To solve this:

1. In the Start() function, set
Quote
UICamera.fallThrough = gameObject;
2. Delete your Update() function. You don't need it. Instead, you get OnDrag:
  1. void OnDrag (Vector2 delta)
  2. {
  3.     Debug.Log("Dragging the object " + delta + " pixels");
  4. }
I also suggest making it even better. NGUI is capable of sending out events to all objects that have a collider, so if you were to put a UICamera script on your main camera that sees your floor, then add a collider to it, and assuming this script was attached to the floor, you would then not even need to set the 'fallThrough' object in Start. You will be getting OnDrag events automatically when you start dragging on the floor. You can then do this:
  1. void OnDrag (Vector2 delta)
  2. {
  3.     Debug.Log("World position of the hit: " + UICamera.lastHit.point);
  4. }

Der_Kevin

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: NGUI: Draggable Camera and Scroll Panel
« Reply #2 on: April 15, 2014, 10:19:47 AM »
Hey!
works out pretty good. thanks for the help!

sadly the camera is jumping around pretty crazy. you can see the changes here:
http://pixelpizza.de/unity/nguidrag_v2.html

i changed the script to this:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ViewDrag : MonoBehaviour {
  5.         Vector3 hit_position = Vector3.zero;
  6.         Vector3 current_position = Vector3.zero;
  7.         Vector3 camera_position = Vector3.zero;
  8.         float z = 0.0f;
  9.        
  10.         // Use this for initialization
  11.         void Start () {
  12.                 UICamera.fallThrough = gameObject;
  13.         }
  14.        
  15.         void OnDrag(){
  16.                 if(Input.GetMouseButtonDown(0)){
  17.                         hit_position = Input.mousePosition;
  18.                         camera_position = transform.position;
  19.                        
  20.                 }
  21.                 if(Input.GetMouseButton(0)){
  22.                         current_position = Input.mousePosition;
  23.                         LeftMouseDrag();        
  24.                 }
  25.         }
  26.        
  27.         void LeftMouseDrag(){
  28.                 current_position.z = hit_position.z = camera_position.y;
  29.                 Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
  30.        
  31.                 direction = direction * -1;            
  32.                 Vector3 position = camera_position + direction;        
  33.                 transform.position = position;
  34.         }
  35. }
  36.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI: Draggable Camera and Scroll Panel
« Reply #3 on: April 16, 2014, 11:22:57 AM »
OnDrag has a Vector2 parameter that you're missing. There is also no need to check for Input.GetMouseButtonDown. Ever. This will always be 'false', so the first part of your OnDrag function will never execute.

If you need to record the position, do it in OnPress.
  1. void OnPress (bool isPressed)
  2. {
  3.     if (isPressed)
  4.     {
  5.         hit_position = UICamera.currentTouch.pos;
  6.         camera_position = transform.position;
  7.     }
  8. }
Bottom line, if you find yourself using Input.anything, you're doing something wrong.