1
\$\begingroup\$

I am trying to make Basic Touch behaviors for my game where i want to

  1. Pan Camera by Single finger touch.
  2. Zoom Camera by pinch With two fingers ofCourse.
  3. Rotate Camera by two fingers rotation.
  4. Rotate camera around an object and move only in x axis using Two fingers y position.

i am having problem in making camera to rotate on X axis but i am not able to do it correctly plz help and it also conflicts with my pan behaviour when input is given

   public float comfortZoneVerticalSwipe_A = 10; 
    public float comfortZoneHorizontalSwipe = 50; 
    public float minSwipeDistance = 100; 

    public float startTime;
    public Vector2 startPos;
    public float maxSwipeTime = 0.5f;

    if (Input.touchCount > 0 && Input.touchCount < 2)
            {
                Touch touch = Input.touches[0];
                switch (touch.phase)
                { //following are 2 cases

                    case TouchPhase.Began: //here begins the 1st case
                        startPos = touch.position;
                        startTime = Time.time;

                        break; //here ends the 1st case


                    case TouchPhase.Moved:

           Vector3 offset = transform.right * (-Input.touches[0].deltaPosition.x * 0.9f) * panSpeed + transform.up * (-Input.touches[0].deltaPosition.y * 0.9f) * panSpeed;
                                Vector3 newTargetOffset = Vector3.ClampMagnitude(offset, maxOffsetDistance);

                                transform.position += newTargetOffset - targetOffset;

                        break;


                    case TouchPhase.Ended: //here begins the 2nd case

                         float swipeTime = Time.time - startTime;
                        float swipeDist = (touch.position - startPos).magnitude;

                              Debug.Log("Mathf.Abs(touch.position.x - startPos.x) " + Mathf.Abs(touch.position.x - startPos.x));

                            Debug.Log("swipe Time " + swipeTime);

                            Debug.Log("swipe Dist " + swipeDist);

                            if (Mathf.Abs(touch.position.x - startPos.x) < comfortZoneVerticalSwipe_A && swipeTime < maxSwipeTime && swipeDist > minSwipeDistance && Mathf.Sign(touch.position.y - startPos.y) > 0)
                        {



                            Debug.LogWarning(transform.rotation.x);
                            transform.rotation = new Quaternion(transform.rotation.x - 5 , transform.rotation.y , transform.rotation.z , transform.rotation.w);
                           if (transform.rotation.x > 0.2)
                           {
                                transform.Rotate(Vector3.right, -2);
                            }
                                Debug.LogError(transform.rotation.x);
                            Debug.LogWarning("Up");

                        }
                            else if (Mathf.Abs(touch.position.x - startPos.x) > comfortZoneVerticalSwipe_A && swipeTime > maxSwipeTime && swipeDist > minSwipeDistance && Mathf.Sign(touch.position.y - startPos.y) < 0)
                            {


                                 transform.rotation = new Quaternion(transform.rotation.x - 5 , transform.rotation.y , transform.rotation.z , transform.rotation.w);
                                if (transform.rotation.x < 0.9)
                                {
                                    transform.Rotate(Vector3.right, 2);
                                }
                                print("down");

                            }

                        break; //here ends the 2nd case
                }
            }
\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

Why are you changing the position of the camera using transform.position += newTargetOffset - targetOffset; ?

While panning, you should only rotate the camera. Also, what is targetOffset?

\$\endgroup\$
3
  • \$\begingroup\$ targetOffset is vector3.zero but my panning is working fine i ma changing camera position to pan it where user wants but on long swipes in y axis i need to change the rotaion also when in case TouchPhase.Ended: \$\endgroup\$ May 24, 2016 at 7:50
  • \$\begingroup\$ @green you must need to place your comments in comment section \$\endgroup\$ May 27, 2016 at 7:49
  • \$\begingroup\$ That is what I intended to do in the first place. However, I didn't have enough points to add comments, so I added here. \$\endgroup\$ May 27, 2016 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .