- Home /
Question by
PhraseUniverse · Feb 24, 2017 at 09:13 AM ·
rotate objecttouch screen
Two finger touch to rotate sprite.
I'm working on a drag n drop script that I want to add two-finger rotate to. The object rotates, but so does all the other objects on the screen that have the script attached to it. I just want to rotate the one object being touched. Any help would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class dragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public static GameObject DraggedInstance;
private float zAngle = 0.0f;
Vector3 _startPosition;
Vector3 _offsetToMouse;
float _zDistanceToCamera;
#region Interface Implementations
public void OnBeginDrag (PointerEventData eventData)
{
DraggedInstance = gameObject;
_startPosition = transform.position;
_zDistanceToCamera = Mathf.Abs (_startPosition.z - Camera.main.transform.position.z);
_offsetToMouse = _startPosition - Camera.main.ScreenToWorldPoint (
new Vector3 (Input.mousePosition.x, Input.mousePosition.y, _zDistanceToCamera)
);
}
public void OnDrag (PointerEventData eventData)
{
if(Input.touchCount > 1)
return;
transform.position = Camera.main.ScreenToWorldPoint (
new Vector3 (Input.mousePosition.x, Input.mousePosition.y, _zDistanceToCamera)
) + _offsetToMouse;
}
public void OnEndDrag (PointerEventData eventData)
{
DraggedInstance = null;
_offsetToMouse = Vector3.zero;
}
void Update()
{
if (Input.touchCount == 2)
{
//Touch rotation
var dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x)*Mathf.Rad2Deg;
var rot = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = rot;
}
}
#endregion
}
Comment
Your answer
Follow this Question
Related Questions
touch screen position rotate turret and shot same time 0 Answers
problem with rotating and scaling a whole imported FBX 0 Answers
Tornado Simulator 0 Answers