- Home /
Multi-touch for camera not working?
Hi, I have a joystick in the Canvas used to move a ball. I want to integrate this camera script: using UnityEngine; using System.Collections;
public class RotateCamera : MonoBehaviour {
void Start (){
Vector3 angles= transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
sensivityX = PlayerPrefs.GetFloat ("camera_sensivity");
sensivityY = PlayerPrefs.GetFloat ("camera_sensivity") / 1.4f;
nono = GameObject.Find("VirtualJoystickContainer (1)").GetComponent<RectTransform>();
}
void FixedUpdate ()
{
if (target && GetComponent<Camera> ()) {
//Zooming with mouse
distance += Input.GetAxis ("Mouse ScrollWheel") * distance;
distance = Mathf.Clamp (distance, minDistance, maxDistance);
if (!RectTransformUtility.RectangleContainsScreenPoint (nono, Input.mousePosition)) {
if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Moved) {
//One finger touch does orbit
touch = Input.GetTouch (0);
if (PlayerPrefs.GetInt ("InvertX") == 0)
x += touch.deltaPosition.x * xSpeed * 0.02f * sensivityX;
else
x -= touch.deltaPosition.x * xSpeed * 0.02f * sensivityX;
if (PlayerPrefs.GetInt ("InvertY") == 0)
y += touch.deltaPosition.y * ySpeed * 0.02f * sensivityY;
else
y -= touch.deltaPosition.y * ySpeed * 0.02f * sensivityY;
}
if (Input.touchCount > 1 && (Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (1).phase == TouchPhase.Moved)) {
//Two finger touch does pinch to zoom
var touch1 = Input.GetTouch (0);
var touch2 = Input.GetTouch (1);
curDist = Vector2.Distance (touch1.position, touch2.position);
if (curDist > lastDist) {
distance += Vector2.Distance (touch1.deltaPosition, touch2.deltaPosition) * pinchSpeed / 10;
} else {
distance -= Vector2.Distance (touch1.deltaPosition, touch2.deltaPosition) * pinchSpeed / 10;
}
lastDist = curDist;
}
}
y = ClampAngle (y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler (y, x, 0);
Vector3 vTemp = new Vector3 (0.0f, 0.0f, -distance);
Vector3 position = rotation * vTemp + target.position;
transform.position = Vector3.Lerp (transform.position, position, cameraSpeed * Time.deltaTime);
transform.rotation = rotation;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
The camera itself is working fine but the problem comes when I want to move my ball and move the camera simultaneously. It's like the camera doesn't support multi-touch, I can drag the joystick but the camera is not responding! The camera is moved by moving your finger outside of the joystick to orbit the ball. It even doesn't work when I hold any part of the screen and try to move the camera. I also have another camera script suffering from the same problem. Can this be fixed? I've been struggling with this for a while now.
the script clearly uses one touch for orbiting and two touches for pinching.
Hi, This script also doesn't work with the joystick. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Camera$$anonymous$$otor : $$anonymous$$onoBehaviour { private void Start() { offset = new Vector3 (0, yOffset, -1f * distance); }
private void Update()
{
if (Swipe$$anonymous$$anager.Instance.IsSwiping (SwipeDirection.Left))
SlideCamera (true);
if (Swipe$$anonymous$$anager.Instance.IsSwiping (SwipeDirection.Right))
SlideCamera (false);
}
private void FixedUpdate()
{
desiredPosition = lookAt.position + offset;
transform.position = Vector3.Lerp (transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
transform.LookAt (lookAt.position + Vector3.up);
}
public void SlideCamera(bool left)
{
if (left)
offset = Quaternion.Euler (0, 60, 0) * offset;
else
offset = Quaternion.Euler (0, -60, 0) * offset;
}
} It only uses one finger swiping. This is the swipe script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public enum SwipeDirection { None = 0, Left = 1, Right = 2, Up = 4, Down = 8, }
public class Swipe$$anonymous$$anager : $$anonymous$$onoBehaviour {
private void Start()
{
instance = this;
}
private void Update()
{
Direction = SwipeDirection.None;
if(!RectTransformUtility.RectangleContainsScreenPoint (nono, Input.mousePosition))
{
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
touchPosition = Input.mousePosition;
}
if (Input.Get$$anonymous$$ouseButtonUp (0))
{
Vector2 deltaSwipe = touchPosition - Input.mousePosition;
if($$anonymous$$athf.Abs(deltaSwipe.x) > swipeResistanceX)
{
// Swipe on the X axis
Direction |= (deltaSwipe.x <0) ? SwipeDirection.Right : SwipeDirection.Left;
}
if ($$anonymous$$athf.Abs (deltaSwipe.y) > swipeResistanceY) {
// Swipe on the Y axis
Direction |= (deltaSwipe.y < 0) ? SwipeDirection.Up : SwipeDirection.Down;
}
}
}
}
public bool IsSwiping(SwipeDirection dir)
{
return (Direction & dir) == dir;
}
}.
Your answer
Follow this Question
Related Questions
Multitouch problem 0 Answers
Multitouch events get stucked. 0 Answers
Help With Multitouch in 2D Windows Store apps 0 Answers
How to get multitouch input using EasyTouch 0 Answers
Simultaneous Multitouch Sends The Touch Over and Over 1 Answer