- Home /
touchscreen and mouse orbit
I have a HP2310 touchscreen and an orbit script. When i use the mouse works perfect, but when i use the finger it doesn't.
function LateUpdate () {
x += Input.GetAxis("Mouse X") * xSpeed *0.02; //x rotation
gameObject.Find("testMouse").guiText.text = Input.GetAxis("Mouse X").ToString();
y -= Input.GetAxis("Mouse Y") * ySpeed *0.02; //y rotation
gameObject.Find("testMouse2").guiText.text = Input.GetAxis("MouseY").ToString();
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance)+ target.position;
transform.rotation = rotation;
transform.position = position;
}
I uses 2 gui text to print the mouse position when i run the project in standalone mode. The results : when using the mouse it prints values beetween -1 and 1 but with finger nothing is printed.
I've notice that the touch reacts to the OnMouseEnter event. How can i modify this to check is mouse entered my object?
Ideas ?
You mean if the touch is over the object? You might need to use Raycasting.
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html
Let met see if I fully understood what you need: you want to touch than drag the touch and when you reach an object during this drag you want to do something?
i want the object to rotate like it does on mouseOrbit script, but using the touch.
You want to same camera effect that $$anonymous$$ouseOrbit does with mouse, but using touch, right? Well it should work right away. I´ve used it a while ago and no modification were needed, I just changed it a bit to get it more smooth to the touch. I´ll search the code I used here and send it to you as soon as I find it...
Answer by rhose87 · Nov 14, 2011 at 01:42 PM
I solved my problem with the folowing code :
function Update(){
var curentX = Input.mousePosition.x;
var differenceX = curentX-startX;
if(differenceX>0)
target.transform.Rotate(0.0, -10*xSpeed*Time.deltaTime*0.1, 0.0,Space.World);
if(differenceX<0)
target.transform.Rotate(0.0, 10*xSpeed*Time.deltaTime*0.1, 0.0,Space.World);
startX = curentX;
differenceX = 0;
}
function Start(){
startX = Input.mousePosition.x;
}
rhose87, i have an error in this code please help me me and you have the same question.. i want to try your code.. i have an error "unknown identifier startX,target,xSpeed,target,xSpeed" thanks for your help
declare them as variables. target is a game object.
var startx; //etc
Answer by yusufulutas · Nov 12, 2011 at 11:37 AM
TouchPhase.Moved and Touch.deltaPosition
I think this will work if you use them.
Example of use:
Touch finger = Input.GetTouch(0);// 0 is first finger
finger.phase == TouchPhase.Moved;
finger.deltaPosition;// travels the path calculates the finger on the screen.
I'm sorry for my English is bad.
This is the correct approach.
The OP is trying to use mouse code for touch input.
He solved his specific issue with a workaround that will work now, but cause issues later if he tries to process more touch input (in my opinion).
Hi @jahroy, @yusufulutas, can you guys please clarify the getTouch approach? How do I edit the existing code?\ Thanks, Garry
@redbeef - Here is the documentation for Input.GetTouch(), which returns a Touch object:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html