Hi, I have problem with the game. The point is that, when I rotate camera all works. But when I just click on the screen camera change rotation. That’s the code (founded on the youtube, and edited by me):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlsDzialaj : MonoBehaviour
{
private Touch initTouch = new Touch();
public Camera cam;
private float rotX = 0f;
private float rotY = 0f;
private Vector3 origRot;
public float rotSpeed = 1f;
public float dir = -1;
void Start()
{
origRot = cam.transform.eulerAngles;
rotX = origRot.x;
rotY = origRot.y;
}
void FixedUpdate ()
{
foreach(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
}
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;
cam.transform.eulerAngles = new Vector3(rotX, rotY, 0f);
}
else if(touch.phase == TouchPhase.Ended)
{
}
}
}
}
Is this code from a tutorial or elsewhere that you’re reusing?
By looking at your code, you’re never setting any values in initTouch. In your TouchPhase.Moved block you then subtract the new touch position from that initTouch position, which I assume is defaulted to (0, 0). Again, I’m guessing, but in your TouchPhase.Began block you probably need to store the initial touch position into initTouch so that your deltas are calculated relative to where the touch starts.
Yup, I founded it on Youtube and edited it. By the way, when I move the screen to rotate, it works. But when I click on the screen camera rotates, but i don’t want that because it’s nervous.