- Home /
How can ı rotate camera with touch?
Hi everyone ı want to rotate camera with touch.
ı tried this logic:
Function Update()
{
İf(Input.GetTouch(0).phase==TouchPhase.Began)
{
var firstposint:Transform;
firstpoint.position=Input.GetTouch(0).position;
}
if(Input.GetTouch(0).phase==TouchPhase.Stationy)
{
var seconpoint:Transform;
seconpoint.position=Input.GetTouch(0).position;
this.camera.transform.rotation.x += seconpoint.position.x - firstpoint.position.x;
this.camera.transform.rotation.y += seconpoint.position.y - firstpoint.position.y;
}
}
this isnt work! ı dont understand ı am getting to first touch position for example firstpoint=20 and while ı was touching to screen ı am getting position of second touch for example seconpoint=40
and ı am setting to them differences to rotation of camera! where ı am wrong? pls help me!
How is your script even building? on line 10:
if(Input.GetTouch(0).phase==TouchPhase.Stationy)
"Stationy" isn't a enum member of TouchPhase, TouchPhase.Stationary would be the correct enum that you're looking for to begin with.
Answer by zharik86 · Oct 09, 2014 at 06:08 AM
Method transform.rotation is Quaternion. For more information about quaternion see docs. I correct your code:
private var firstpoint:Vector3; //change type on Vector3
private var secondpoint:Vector3;
private var xAngle: float = 0.0; //angle for axes x for rotation
private var yAngle: float = 0.0;
private var xAngTemp: float = 0.0; //temp variable for angle
private var yAngTemp: float = 0.0;
function Start() {
//Initialization our angles of camera
xAngle = 0.0;
yAngle = 0.0;
this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0);
}
function Update() {
//Check count touches
if(Input.touchCount > 0) {
//Touch began, save position
if(Input.GetTouch(0).phase == TouchPhase.Began) {
firstpoint = Input.GetTouch(0).position;
xAngTemp = xAngle;
yAngTemp = yAngle;
}
//Move finger by screen
if(Input.GetTouch(0).phase==TouchPhase.Moved) {
secondpoint = Input.GetTouch(0).position;
//Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0 / Screen.width;
yAngle = yAngTemp - (secondpoint.y - firstpoint.y) *90.0 / Screen.height;
//Rotate camera
this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0);
}
}
}
Attach this script at Main Camera on your scene. Of course, you may to use Touch.deltaPosition, but I write common method. I hope, that it will help you.
@zharik86 how this works. the camera rotate from some point. If the perspective camera is Orthographic, works good?.
It work's Pretty nicely just need to add your personal constraits
Vector3 FirstPoint;
Vector3 SecondPoint;
float xAngle;
float yAngle;
float xAngleTemp;
float yAngleTemp;
void Start () {
xAngle = 0;
yAngle = 0;
this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0);
}
void Update () {
if(Input.touchCount > 0){
if(Input.GetTouch(0).phase == TouchPhase.Began){
FirstPoint = Input.GetTouch(0).position;
xAngleTemp = xAngle;
yAngleTemp = yAngle;
}
if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved){
SecondPoint = Input.GetTouch(0).position;
xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
}
}
}
Here's the code provided in C# format
It is works fine! Thank you man. I clamped yAngle in this line for avoid overflip camera. this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
Bro can you please tell me how to make the camera rotate only in between fixed angles in y axis .. this code is super helpful but the camera inverts scene in y axis if rotated too much .. Please Help $$anonymous$$e Bro.
Although this is pretty old, I am currently using a tweaked version of this code, apparently one of the few reasonable options to have touch inputs working on webgl. Despite working pretty good on iOS, it's got a bit of an offset on Android: touch.phase == began angles for both axis seems not to be stored, and every time the rotation of the camera starts from a greater x or y position than the one my finger is holding onto. It seems to have two possible reasons: a problem of sensibility (touching the screen extremely lightly seems to occasionally avoid this "jump") and a began phase that starts from the edges of the screen: when my finger is stationary in the middle, and makes little movements, jumps are almost unnoticeable.
I am trying to clamp the xAngTemp in the began phase as follows:
firstpoint = Input.GetTouch(0).position; xAngTemp = $$anonymous$$athf.Clamp(xAngle, $$anonymous$$imumX, maximumX);
I still can't make it work. Any suggestion?
Answer by MonkeyPuzzle · Feb 28, 2017 at 07:50 AM
This code really helped me a lot! I tried a few different things and this turned out to be the right answer for my project. I did have one issue. When I rotated my camera on the y-axis until it was upside down, the x-axis was reversed. Here is my update to solve the issue. I am also clamping the values to be 0 to 360. My code is also for rotating the Player, not a camera, but that can be easily changed.
My project is written in c#. @zharik86
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class touchCam : MonoBehaviour {
private Vector3 firstpoint;
private Vector3 secondpoint;
private float xAngle = 0.0f; //angle for axes x for rotation
private float yAngle = 0.0f;
private float xAngTemp = 0.0f; //temp variable for angle
private float yAngTemp = 0.0f;
private GameObject playRot;
void Start() {
dtext=GameObject.Find("debugText").GetComponent<Text>();
firstpoint = new Vector3 (0, 0, 0);
secondpoint = new Vector3 (0, 0, 0);
playRot = GameObject.Find ("Player");
xAngle = 0.0f;
yAngle = 0.0f;
transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
}
void Update() {
//Check count touches
if (PlayerController.instance.TabletMode == 1) {
if (Input.touchCount > 0) {
//Touch began, save position
if (Input.GetTouch (0).phase == TouchPhase.Began) {
firstpoint = Input.GetTouch (0).position;
xAngTemp = xAngle;
yAngTemp = yAngle;
}
//Move finger by screen
if (Input.GetTouch (0).phase == TouchPhase.Moved) {
secondpoint = Input.GetTouch (0).position;
//Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;
if (yAngle < 0)
yAngle +=360;
if (yAngle > 360)
yAngle -=360;
if (yAngle > 90 && yAngle < 270)
xAngle = xAngTemp - (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
else
xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
if (xAngle < 0)
xAngle +=360;
if (xAngle > 360)
xAngle -=360;
transform.rotation = Quaternion.Euler (yAngle, xAngle, 0.0f);
}
}
}
}
}
Answer by rajannath · Dec 15, 2017 at 02:09 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class rotate : MonoBehaviour {
float rotSpeed =20;
void OnMouseDrag ()
{
float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.DegZRad;
float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.DegZRad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
}
Note is $$anonymous$$athf.Deg2Rad and not $$anonymous$$athf.degZRad;
Answer by kaksaveksaka · Oct 23, 2021 at 08:16 AM
{
Vector3 FirstPoint;
Vector3 SecondPoint;
float xAngle;
float yAngle;
float xAngleTemp;
float yAngleTemp;
public float ymax;
public float ymin;
void Start()
{
xAngle = 0;
yAngle = 0;
}
void Update()
{
if (Input.touchCount > 0)
{
bool yclamp = false;
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
FirstPoint = Input.GetTouch(0).position;
xAngleTemp = xAngle;
if(!yclamp)
yAngleTemp = yAngle;
}
if (yAngle > ymax && yAngle - yAngleTemp > 0 || yAngle < ymin && yAngle - yAngleTemp < 0)
{
yclamp = true;
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
SecondPoint = Input.GetTouch(0).position;
xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
// yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
}
}
else
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
SecondPoint = Input.GetTouch(0).position;
xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
yclamp = false;
}
Debug.Log(yAngle);
}
}
}
}
@Jaskirat_Games this works smoothly for me to clamp y axis
Answer by Jaskirat_Games · Aug 31, 2019 at 06:49 AM
The above code nearly solved my problem but could anybody help me to clamp the rotation in x and y axis.@zharik86 .
xAngle = $$anonymous$$athf.clamp(xAngle,-180f,180f); you can change the restrictions based on your requirements and can use the same code for yAngle as well by substituting xAngle with yAngle
Your answer
Follow this Question
Related Questions
Rotate object after swipe according to swipe direction 0 Answers
Exporting to Touch Screen Monitors or Touch Screen Displays 0 Answers
Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 1 Answer
Input Touch.position 'inverted' relative to screen? 1 Answer
Rotate player to LookAt a touch position 2 Answers