Touchscreen problem, rotate around an objet and using GUIs...

Hi,

I’m using my architectural viewer with a touchscreen. (single touch, PC, coding in C#).

I’ve got few problems and I can’t find a solution.

So, my viewer has a complete GUI system, (buttons, labels, etc), and a camera which rotates aroud my object.

I had to modify my rotateCam script to make it works on the touchscreen. I use now a “Screen.width/2 - input.mousePosition.x” to define the vector of my rotation.

Here is the script:

if (Input.GetMouseButton(0))
            {

                xDeg = -(Screen.width / 2 - Input.mousePosition.x) * SpeedCoef * xSpeed;
                yDeg = (Screen.height / 2 - Input.mousePosition.y) * SpeedCoef * ySpeed;

                
                // Make sure that the yDeg is still in the Y limits
                yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);

                //Use the AmtToRotate to rotate the camera
                desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
                currentRotation = transform.rotation;
                //Add a damping
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;

            }
            if (!Input.GetMouseButton(0))
            {
                currentRotation = transform.rotation;
                rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
                transform.rotation = rotation;
            }

First problem, when i’m clicking on the screen (or touch the touchscreen) my script take that mouse position as the new position and quickly rotates the camera. Then when I drag my mouse/finger, everything rotates as i want and if i release the mouse button or the screen it stop smoothly as I wanted too.

So, in my mind, I would like my script to use the first touch of the screen as a movement = 0 and use the dragging only to add a rotation to that start position.

Second problem, (which is related to the first I think, but there is maybe a way to solve it first as it is related to the GUIs) is that in my viewer I’ve got buttons such as home button or screenshot button which needs the cam to do not move, or take that as a rotation.
Is it possible to define different zones on the screen ? Like If mouse on the gui zone, the rotateCam script stops.

I tried the if (gui.button) → stop the rotation script then if button released → rotation script on. It doesn’t work obviously…

I really need some help, I’m stuck on that problem since a while, and I’ve got few projects wich requires the touchscreen.

Any help would be really much appreciated.

BooBi

ps sorry for the double post but I wanted to summarize my problems in one post.

Use Input.GetMouseButtonDown (0) before the if checking Input.GetMouseButton (0). Use this to, on the frame where the button gets pressed, reset the rotation base to the current mouse coordinates - rather than go through the regular rotate code. So:

if (Input.GetMouseButtonDown (0))
{
// We started dragging - reset rotation delta
}
else if (Input.GetMouseButton (0))
{
// The rotation code
}
else
{
// Your code handled in !Input.GetMouseButton (0) - no need to check for it at this point as we already know that Input.GetMouseButton (0) is false.
}

thank’s for that, I do understand your logic and it really makes sense but how do you do to reset the rotation delta ?
I’ve got that problem since a looooong time, it interfer aswell with my home position, I had a previous post (answerless) about that reset rotation delta.

Ah I see. Didn’t look that close at your code at first, but you’re using the absolute cursor position rather than the mouse delta (how much the mouse was moved since last frame). I’d suggest you switch to using this in stead - use Input.GetAxis (“Mouse X”) and Input.GetAxis (“Mouse Y”) - see the examples for more info.

ah… that’s my main problem, I had to change that code to the second one to make it work on the touchscreen:

                //Set the AmtToRotate
                xDeg += Input.GetAxis("Mouse X") * xSpeed * SpeedCoef;
                yDeg -= Input.GetAxis("Mouse Y") * ySpeed * SpeedCoef;

to

xDeg = -(Screen.width / 2 - Input.mousePosition.x) * SpeedCoef * xSpeed;
                yDeg = (Screen.height / 2 - Input.mousePosition.y) * SpeedCoef * ySpeed;

Isn’t that possible with the abolute position too?

If I use the first one (my standard viewer works with the MouseX/Y), nothing moves on the touchscreen… The switch to the mouseposition was the only way i found to make it works…

Can I ask how would you do the reset when i use the GetAxis type ?

up !!

It sounds like your touch screen hardware is not actually transmitting mouse movement, but doing some kind of direct set of the position.

To get around this, you could calculate your own mouse delta by storing the last mouse position every frame and at the beginning of a mouse drag frame, subtract the two - giving you the delta.

Ex:

Vector2 m_LastMousePosition;
void Update ()
{
	if (Input.GetMouseButton (0)  !Input.GetMouseButtonDown (0))
	{
		Vector2 mouseDelta = Input.mousePosition - m_LastMousePosition;
			// Mouse delta is how much the mouse moved since last frame - the base on which you calculate how much to rotate
		// ...
	}
	else if (!Input.GetMouseButton(0))
	{
		// ...
	}
	
	m_LastMousePosition = Input.mousePosition;
}

Hi Angry Ant,

I seem to have same issue with my touchscreen. I’ve tried to implement your solution into my JS script, but Im not getting the results I want.

The issue at hand is I want to translate objects if an gameobject is hit and has a specific tag. This does not happen. Below is the code running.

First raytracer from camera

//Variables for tracking where the mouse clicks
static var _ray : Ray;

var m_LastMousePosition : Vector2;

function Update ()
{
	if (Input.GetMouseButton (0)  !Input.GetMouseButtonDown (0))
	{
		var mouseDelta : Vector2 = Input.mousePosition - m_LastMousePosition;
	}
	m_LastMousePosition = Input.mousePosition;
	_ray = camera.ScreenPointToRay (mouseDelta);
}

Then code on the object I want to click.

function Update ()
{
	//Tracks the ray going from the camera to the mousePosition
	if(Input.GetMouseButtonDown(0))
	{
		
		var hit : RaycastHit;
		var test = Physics.Raycast(RayTracer._ray, hit);

		//DEBUG STUFF
                print(test);
		print(RayTracer._ray);
		print(hit);
		print("Menu: " + menu + ", Object name: " + objectName + ", Tag: " + hit.transform.tag);
		
		if (test)
		{
			if (hit.transform.tag == objectName  menu == false)
			{
				OpenMenu();
				menu = true;
			}
			
			if (hit.transform.tag == objectName  menu == true)
			{
				CloseMenu();
				menu = false;
				
			}
		}
	}
}

No matter where I click the same direction occur. It don’t seem to update:/
CONSOLE LOG:
Origin: (21.4, 39.8, 42.5), Dir: (-0.5, -0.9, -0.1)
UnityEngine.MonoBehaviour:print(Object)
ObjectMenu:FixedUpdate() (at Assets/_Scripts/ObjectMenu.js:50)

My code did “work” before putting in your solution, but as BooBi explained it is only the second click which activates anything on last hit position.

Thanks in advance:)