Rotation by touch

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)
            {
           
            }
        }
    }
}

Close this post (no reply beacuse it has wrong title (?))

No response barely one hour after you posted the thread? It all seems perfectly normal.

I suggest that you change the title again to one that describes best your problem. :slight_smile:

1 Like

I know, but when nobody replies, it means nobody will help. I know because i have no replied posts (check if you want).

You have to wait a bit before deciding that nobody wants to help. :slight_smile:

Ok, but, can you help me?

Not right now, it’s late and I’m tired; but others could.

What would you want the camera to do when you click on the screen exactly?

You should also post your entire code, including the other scripts this script depend on (Touch?) so that people can test them and find a solution. :slight_smile:

I have code to rotate camera by touch. It works. But I have one problem. When I touch the screen, camera rotates to other place.

Ok, can somebody help?

What do you mean by this? I can only guess.

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.

Ok, then I guessed correctly and I think the solution I suggested will work.

It doesn’t worked for me.

As I said before:

If you want useful help then you need to post details.

So, code works. All works. But when i just click on the screen, camera rotates randomly. And that’s my post details.

Who can help me

You haven’t given details.

Exactly what changes did you make, and what effect did they have?

I don’t remember

because u use save value
rotx and deltax
rotY and deltay
change as this

rotX -= deltay

rotY += deltax