• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by segment · Jan 28, 2014 at 02:44 PM · fpstouchtouches

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Infinite_Gamer · Jan 28, 2014 at 06:32 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image segment · Jan 29, 2014 at 11:03 AM 0
Share

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!

avatar image supernat · Jan 29, 2014 at 08:13 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i get rid of touch lag between touch ending on device and action in game? Or is it possible, on Unity Remote 4 using Nexus 7 2012 with if (Input.touchCount > 0) for touch input? 1 Answer

if I lock iPhone fps to 24fps, can I get more accurate touch timings than that? 3 Answers

Detect Android device touch points amount. 0 Answers

Swipe Control Issue.. Need help 1 Answer

Use touch to spawn a prefab 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges