Hello I am trying to create a code that rotates the camera in a 360 degree rotation where you can drag and look up and down and side to side kind of like a 360 video on youtube. But with this current script I am running into a issue that it seems to bring me back to a certain position. it snaps every time you tap it again I want it to be a fluid rotation and where you can take your finger off and pause and look around.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swiper : MonoBehaviour
{
private Touch initTouch = new Touch();
public Camera Camera;
private float rotX = 0f;
private float rotY = 0f;
private Vector3 origRot;
public float rotSpeed = 1f;
public float dir = -1;
void Start()
{
origRot = Camera.transform.eulerAngles;
rotX = origRot.x;
rotY = origRot.y;
}
void FixedUpdate()
{
foreach(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
initTouch = touch;
}
else if(touch.phase == TouchPhase.Moved)
{
float deltaX = initTouch.position.x - touch.position.x;
float deltaY = initTouch.position.y - touch.position.y;
rotX -= deltaX * Time.deltaTime * rotSpeed * dir;
rotY += deltaY * Time.deltaTime * rotSpeed * dir;
Mathf.Clamp(rotX, -45f, 45f);
Camera.transform.eulerAngles = new Vector3(rotX, rotY, 0f);
}
else if(touch.phase == TouchPhase.Ended)
{
initTouch = new Touch();
}
}
}
}
Show your modified code after you followed Kurt-Dekker’s suggestion.
By the way, this code looks like it might do weird things if there is ever more than 1 touch at a time. (You overwrite the initTouch variable every time some touch begins, even if there are other touches already in progress, and all active touches separately but simultaneously try to rotate the view relative to that.)
hi do you have a solution for this? I also found this exact code in a tutorial from 2015 , and is the first one that doesn’t use the already written scripts form some character form unity store but when I apply it to an image in canvas, it makes my whole screen touchable on my device and even my character starts spinning when I try to use the joystick and on laptop when I click play in unity the joysticks works fine as intended but the camera wont rotate it seems to ignore the image object size in canvas and make the whole screen touchable on device
Regardless of what you do, I suggest making a fresh empty scene with NOTHING but a camera and this controller and a few graphics primitive objects to look at. Work through the script slowly, using Debug.Log() to figure out what various values are, if code you think is running is even actually running, and get the camera working. Only once the camera is working PERFECTLY, then consider introducing it to the rest of your game. You can’t engineer “all the things” at once. Break the problem down.
You mean the multi-touch issue? Several things you could do, but the simplest quick fix is probably to average all touches into a single point and treat them as a single touch at that point. Still distinguish between “zero touches” and “one or more touches”, of course.
I did that , I opened a new project to setup a proper camera not using a real character either just a cilinder on a plane got the joystick part down and the player has fluid movements my only issue is the camera found lots of tuts ( mostly old) some work some don’t some are just weird I mean camera moves strange and others use a right joystick for camera movements I need the right joysticks for other actions tried a mouse aim camera but that works with mouse, maybe I should try again the tut with the camera movements on right joystick but last time I moved that to my canvas image camera only moved up and down and slopy
hmm I understand I was thinking of that like an invisible object above the players head that will handle only the input for the camera movement left right up down ( since i tried with an invisible image that handles half the device screen did not work was thinking of shrinking the input range for the camera ) but since i’m fairly new at this how would I apply that in code?