- Home /
One more question about touches
Hi, I'm working on FPS game and there's a crosshair(and it's texture) on the screen. Now I need to move it with touch and drag. I have already searched through forums but nothing helps for me. Here's my method:
protected void processMove(Vector3 delta)
{
#if UNITY_ANDROID || UNITY_IPHONE
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
var t = Input.GetTouch(0);
touchId = t.fingerId;
startTouchPos = t.position;
}
var touch = Input.GetTouch(touchId);
if(touch.phase == TouchPhase.Moved)
{
var x = touch.deltaPosition.x * Time.deltaTime * 10f;
var y = touch.deltaPosition.y * Time.deltaTime * 10f;
//var dest = Camera.main.ScreenToWorldPoint(
//new Vector3(startTouchPos.x, startTouchPos.y, 1000));
crosshairPosition = new Rect(x, y, crosshairTexture.width, crosshairTexture.height);
}
}
#endif
}
I'm calling this method in Update(). What am I doing wrong?
Thank you.
I don't know that much about touch because I have never used it before.But if I was you I would take a look at This.It Is in C# but I think you could still learn from it.
Hope this helps.
Answer by supernat · Jan 29, 2014 at 07:29 AM
touch.deltaPosition is the delta from the last frame, not the delta from the first time the touch started. So you're setting var x, y to roughly the same value every frame. I'm guessing that when you drag your finger faster, the crosshair moves a small amount and returns to center?
http://docs.unity3d.com/Documentation/ScriptReference/Touch-deltaPosition.html
You instead probably want to use the difference from start:
var x = touch.position.x - startTouchPos.x;
var y = touch.position.y - startTouchPos.y;
I'm not sure why you would multiply by Time.deltaTime, so probably need to remove that.
Thank you, supernat, fixed! By multiplaying by Time.deltaTime I wanted to add more flexibility..how do I do it in my case then? And whatis this goes to is to rotate my gameObject if cursor is near the edges of screen. I have added a Rect crosshair$$anonymous$$oveZone
wich indicated where I shouldn't rotate gameObject and tried this code http://pastebin.com/aJxeTV5n. Object rotates but very fast and weapon rotates around itself. How would be better to rotate gameObject if cursor(crosshairPosition) is near the screen edges? Thank you!
No problem! So I think I understand, you want the game object to rotate slower, so actually that is where you need to use the Time.deltaTime. I think you always want the crosshairs to be under the person's fingers, so don't use time there.
Actually, anytime you are working with a rate, i.e. something moving at X degrees/sec or X feet/sec, etc, you want to multiply by Time.deltaTime. This ensures that people with devices where frame rates change will see the same rate of change. The only exception to this is if you're moving something within the FixedUpdate() function which always moves as the same time intervals. The time interval can be retrieved with the Time.fixedDeltaTime in those cases.
I just looked at your pastebin code, and you are setting the localEulerAngles to the crosshair position. In essence, you are telling the object to rotate at a rate of X degrees/frame where frame times can change. Just multiply the x/y values by Time.deltaTime and then if it moves too slow, multiply that by a larger constant like 2, 3, 4, etc, to get the rotation rate you want. You could also move this code to the FixedUpdate() method and just multiply the crosshair x/y values by a small constant (like 0.1, 0.2, etc) without deltaTime at all.
Your answer
Follow this Question
Related Questions
Understanding UnityEngine.Input.touches 0 Answers
Emulating screen touch on devices 1 Answer
Detect Android device touch points amount. 0 Answers
Swipe Control Issue.. Need help 1 Answer