Hello. I am having very big issue with this script. Rotation is working fine as i expected but there is one problem that pull my hair off. Whenever i press on screen with my finger obj is reverse back to the same position that i start rotating from. Please help me. Thank you.
using System;
using UnityEngine;
public class touch : MonoBehaviour
{
public GameObject Obj = null;
public float minX = -360.0f;
public float maxX = 360.0f;
public float minY = -45.0f;
public float maxY = 45.0f;
public float sensX = 100.0f;
public float sensY = 100.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
float MouseX;
float MouseY;
void Update()
{
var x = Input.GetAxis(“Mouse X”);
var y = Input.GetAxis(“Mouse Y”);
if (x != MouseX || y != MouseY)
{
rotationX += x * sensX * Time.deltaTime;
rotationY += y * sensY * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, minY, maxY);
MouseX = x;
MouseY = y;
Obj.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
}
The problem is that on a touch device, the mouse position reported warps instantly from wherever the last touch was, to where the finger goes down now. That gives you a huge MouseX (delta) that your code applies to undo most or all of the rotation it did before (and same for MouseY).
To fix this, keep track of whether the mouse was down on the last frame, just like you keep track of the previous MouseX and MouseY. Now, when the mouse goes down (i.e. it’s down now and wasn’t before), set MouseX and MouseY to your the current mouse position right away.
Sorry, I’m about to dash off for a 7-hour car trip… and the wifi in the car is terrible. @Kiwasi , perhaps you have time to show him the path?
@Goldensnitch , I will say that you won’t get very far with Unity without being able to do at least basic coding. Spend the time going through the tutorials in the Learn section (see the link at the top of this page). Also work through learncs.org. These skills are valuable (and, you may discover, a lot of fun too!).
You want to start from the beginning. What behaviour are you trying to create? How should the touch position relate to the objects rotation. What should happen when the touch is moved or released?
It’s not immediately obvious what you are trying to do here.
I think he just wants it so that, when he drags his finger to the left, the object rotates to the left, and then stays there after the touch is released (and can be rotated more by another drag).
Try this. It’s actually simpler than I was thinking, and more like what @Kiwasi said: you just need to skip doing any updating on the frame when the mouse button (touch) first goes down. I also removed some of the extra stuff that had no business being there, just to simplify things a bit.
using System;
using UnityEngine;
public class touch : MonoBehaviour
{
public GameObject Obj = null;
public float minY = -45.0f;
public float maxY = 45.0f;
public float sensX = 100.0f;
public float sensY = 100.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
void Update() {
if (!Input.GetMouseButtonDown(0)) {
var x = Input.GetAxis("Mouse X");
var y = Input.GetAxis("Mouse Y");
rotationX += x * sensX;
rotationY += y * sensY;
rotationY = Mathf.Clamp(rotationY, minY, maxY);
Obj.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
}
I think that work… But when i try on my device… Work only TAPTOMOVE script… And the camera rotation not work… Where is my error? Actually I insert script rotation on object that I want to move… I try to insert on camera? Thanks for help