Question by
perrygamesstudio · Jun 16, 2019 at 11:24 PM ·
mobiletouchtouch controlstouchscreen
Rotate object by touch using Angles?
I am trying to rotate object using touch, I would like it to move from where it is touched from outer edge to where it gets dragged, while rotating around another object as the pivot. I am only trying to rotate its y axis. I was using angles, but problem is depending where I start the touch it will rotate the wrong way. I am converting all points to screen points.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenControlls : MonoBehaviour
{
public GameObject cam;
public float rotSpeed = 0.5f;
private Vector3 startRot;
private Touch initTouch = new Touch();
private Touch touch;
public GameObject pivotpos;
private Vector3 pivotposscreenspace;
private Vector3 currentpos;
private float angle;
private Vector3 initialpos;
private float originalangle;
// Update is called once per frame
void Update()
{
if(Input.touchCount==1)
{
touch = Input.touches[0];
if(touch.phase == TouchPhase.Began)
{
initTouch = touch;
initialpos = Camera.main.ScreenToViewportPoint(initTouch.position);
originalangle = cam.transform.eulerAngles.y;
}
else if (touch.phase == TouchPhase.Moved)
{
pivotposscreenspace = Camera.main.WorldToViewportPoint(pivotpos.transform.position);
currentpos = Camera.main.ScreenToViewportPoint(touch.position);
angle = (Vector3.SignedAngle(currentpos,initialpos, pivotposscreenspace))*rotSpeed;
cam.transform.eulerAngles = new Vector3(startRot.x,originalangle+angle,startRot.z);
}
// }
else if (touch.phase == TouchPhase.Ended)
{
initTouch = new Touch();
}
}
}
}
touchcontrol.jpg
(16.1 kB)
Comment
I would like to be able to continuously spin the object any degrees and in either direction.
Your answer
Follow this Question
Related Questions
ScreenToWorldPoint on One Axis? 0 Answers
Touch screen not working on Android build? 0 Answers
touch of the screen interacts along with buttons on the screen 1 Answer
When The Screen Is Touched Jump 0 Answers