I’ve got a game object in my scene that when I move my finger across the screen, I want the object to rotate in that direction. It’s a cube on screen, and sliding my finger around on the screen should rotate the cube about the center point, but not move the cube’s position. It should only rotate as long as they are actively swiping.
I’m basically trying to replicate 3D minesweeper.
I’m very familiar with C# development but I have almost no experience with unity or 3d development.
You need something like this:
And this:
https://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate
Good luck. This is not very easy to get right.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class CharacterRotating : MonoBehaviour
{
[SerializeField]
private Camera cam;
[SerializeField]
private Quaternion defaultAvatarRotation;
[SerializeField]
private float slowSpeedRotation = 0.03f;
[SerializeField]
private float speedRotation = 0.03f;
private const string AVATAR_TAG = "avatar_model";
private bool isRotating = false;
private RaycastHit hit;
void Awake()
{
Camera[] cameraList = FindObjectsOfType(typeof(Camera)) as Camera[];
foreach (Camera camObj in cameraList)
{
if(camObj && camObj.tag == "Untagged")
{
cam = camObj;
}
}
defaultAvatarRotation.y = 180.0f;
}
// Update is called once per frame
void Update()
{
MouseButtonDown();
MouseButotnUp();
if (Input.GetMouseButton(0) && isRotating)
{
RaycastHit dragingHit;
#if UNITY_EDITOR
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
#elif UNITY_ANDROID
Ray ray = cam.ScreenPointToRay(Input.touches[0].position);
#endif
if(Physics.Raycast(ray, out dragingHit) && dragingHit.collider.gameObject == hit.collider.gameObject)
{
if(hit.collider.tag == AVATAR_TAG && transform == hit.collider.transform)
{
#if UNITY_EDITOR
float x = -Input.GetAxis("Mouse X");
#elif UNITY_ANDROID
float x = -Input.touches[0].deltaPosition.x;
#endif
transform.rotation *= Quaternion.AngleAxis(x * speedRotation, Vector3.up);
}
}
}
else
{
if(transform.rotation.y != defaultAvatarRotation.y)
{
SlowRotation();
}
}
}
private void MouseButtonDown()
{
if(Input.GetMouseButtonDown(0))
{
#if UNITY_EDITOR
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
#elif UNITY_ANDROID
Ray ray = cam.ScreenPointToRay(Input.touches[0].position);
#endif
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == AVATAR_TAG && transform == hit.collider.transform)
{
isRotating = true;
}
}
}
}
private void MouseButotnUp()
{
if(Input.GetMouseButtonUp(0))
{
isRotating = false;
hit = new RaycastHit();
}
}
private void SlowRotation()
{
transform.rotation = Quaternion.Slerp(transform.rotation,
defaultAvatarRotation,
slowSpeedRotation * Time.deltaTime);
}
}
If you need I can make a small test project for more understanding of this code.
1 Like
Simple way:
public Transform cube;
void Update()
{
if (Input.touchCount == 1)
{
// GET TOUCH 0
Touch touch0 = Input.GetTouch(0);
// APPLY ROTATION
if (touch0.phase == TouchPhase.Moved)
{
cube.transform.Rotate(0f, touch0.deltaPosition.x, 0f);
}
}
}
10 Likes
BYELIK:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class CharacterRotating : MonoBehaviour
{
[SerializeField]
private Camera cam;
[SerializeField]
private Quaternion defaultAvatarRotation;
[SerializeField]
private float slowSpeedRotation = 0.03f;
[SerializeField]
private float speedRotation = 0.03f;
private const string AVATAR_TAG = "avatar_model";
private bool isRotating = false;
private RaycastHit hit;
void Awake()
{
Camera[] cameraList = FindObjectsOfType(typeof(Camera)) as Camera[];
foreach (Camera camObj in cameraList)
{
if(camObj && camObj.tag == "Untagged")
{
cam = camObj;
}
}
defaultAvatarRotation.y = 180.0f;
}
// Update is called once per frame
void Update()
{
MouseButtonDown();
MouseButotnUp();
if (Input.GetMouseButton(0) && isRotating)
{
RaycastHit dragingHit;
#if UNITY_EDITOR
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
#elif UNITY_ANDROID
Ray ray = cam.ScreenPointToRay(Input.touches[0].position);
#endif
if(Physics.Raycast(ray, out dragingHit) && dragingHit.collider.gameObject == hit.collider.gameObject)
{
if(hit.collider.tag == AVATAR_TAG && transform == hit.collider.transform)
{
#if UNITY_EDITOR
float x = -Input.GetAxis("Mouse X");
#elif UNITY_ANDROID
float x = -Input.touches[0].deltaPosition.x;
#endif
transform.rotation *= Quaternion.AngleAxis(x * speedRotation, Vector3.up);
}
}
}
else
{
if(transform.rotation.y != defaultAvatarRotation.y)
{
SlowRotation();
}
}
}
private void MouseButtonDown()
{
if(Input.GetMouseButtonDown(0))
{
#if UNITY_EDITOR
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
#elif UNITY_ANDROID
Ray ray = cam.ScreenPointToRay(Input.touches[0].position);
#endif
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == AVATAR_TAG && transform == hit.collider.transform)
{
isRotating = true;
}
}
}
}
private void MouseButotnUp()
{
if(Input.GetMouseButtonUp(0))
{
isRotating = false;
hit = new RaycastHit();
}
}
private void SlowRotation()
{
transform.rotation = Quaternion.Slerp(transform.rotation,
defaultAvatarRotation,
slowSpeedRotation * Time.deltaTime);
}
}
If you need I can make a small test project for more understanding of this code.
Hey Byelik, I know this was posted a while ago but I’m interested in doing the same thing, is your offer to make a sample project still on the table?
private Vector2 startPos,direction;
private float multiplierY=0.02f;
private float multiplierX=0.02f;
void Awake()
{
instance=this;
}
void Update()
{
if(Input.touchCount>0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
direction=Vector3.zero;
break;
case TouchPhase.Moved:
direction = touch.position - startPos;
if(direction.magnitude>0.05f)
{
// var localAngles=this.transform.localEulerAngles;
// localAngles.y+=direction.y*multiplierY;
// this.transform.localEulerAngles=localAngles;
var globalAngles=this.transform.eulerAngles;
globalAngles.y+=-direction.x*multiplierX;
this.transform.eulerAngles=globalAngles;
var dotVal1= Vector3.Dot(Camera.main.transform.forward,this.transform.right);
var dotVal2= Vector3.Dot(Camera.main.transform.forward,this.transform.up);
var dotVal3= Vector3.Dot(Camera.main.transform.right,this.transform.up);
// Debug.Log("Value"+(dotVal>0?true:false));
// MyDebug.Log("Value"+(dotVal>0?true:false));
this.transform.localRotation*=Quaternion.Euler(0,-direction.y*multiplierY*(dotVal1>0?-1:1)*(dotVal1>0?-1:1)*(dotVal3>0?-1:1),0);
}
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
break;
}
}
}
Try out this. It will work properly just like swipe to rotate on phones. Just drag this object on the gameobject you want to rotate using swipe
Here’s my contribution from a few years back, full package, scripts, scenes, setup, etc.
6201992–680924–RotateZViaDrag.unitypackage (20 KB)