Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 TheEngineer · Feb 05, 2014 at 05:32 PM · rotationtouchanglelookatdelta

How to get the growth of an angle from touch

I am trying to figure out how to get the delta angle of a touch. To elaborate, I want to know by how much my angle increased or decreased based on the movement of my finger across the screen.

I've included an image for clarification.

alt text

T1 is the touch. If T1 increases along the tangent of the point (0, 0), the delta is ZERO as the angle is not changing. If T1 increases towards the bottom of the screen and the movement is constant, then the growth is +1 per frame, or however fast the finger is moving. I already know how to do this for plain position, i.e Delta Touch, but I'm not sure how to do it with regards to an angle.

Any help or ideas are most welcome :)

example.jpg (9.1 kB)
Comment
Add comment · Show 3
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 Graham-Dunnett ♦♦ · Feb 05, 2014 at 05:33 PM 0
Share

arctan will give you the angle. Just compute the angle every frame, and then subtract the angle from the last frame and you'l have a delta angle.

avatar image TheEngineer · Feb 05, 2014 at 05:57 PM 0
Share

I would like to add - in case it's not understood, that as soon as the finger stops moving, the delta goes back to zero.

avatar image TheEngineer · Feb 05, 2014 at 06:01 PM 0
Share

This may be a dumb question, but how do I subtract from the last frame?

1 Reply

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

Answer by robertbu · Feb 05, 2014 at 05:50 PM

You don't say if your origin is in screen or world coordinates. If it is in world coordinates, you will need to do a Camera.WorldToScreenPoint() so that your touch and origin are in the same coordinate system. As for calculating the angle, you can use Mathf.Atan2(). Pseudo code:

 var dir = touchPos - originPos;
 var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

'angle' will be zero when 'dir' is equal to Vector3.right. I don't know how you are using this angle, but Mathf.DeltaAngle() will give you change between two angles either from some starting angle or from frame to frame.

Comment
Add comment · Show 5 · 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 TheEngineer · Feb 05, 2014 at 06:33 PM 0
Share

I still don't know how to record the angle from the last frame and use it in the next frame.

avatar image robertbu · Feb 05, 2014 at 07:16 PM 0
Share

At the top of your file put:

 private var lastFrameAngle : float;

When your touch goes down, calculate the starting lastFrameAngle:

 var dir = touchPos - originPos;
 var lastFrameAngle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;

When the the touch moves:

 dir = touchPos - originPos;
 var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
 var angleChange = $$anonymous$$athf.DeltaAngle(lastFrameAngle, angle);
 lastFrameAngle = angle;
avatar image TheEngineer · Feb 05, 2014 at 08:51 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class look_test : $$anonymous$$onoBehaviour
 {
     
     private float previous_angle;
     private Vector2 direction;
     public Vector2 position = Input.GetTouch(0).position;
     private float angle;
     private float angle_delta;
     
     void Update () 
     {
         foreach(Touch touch in Input.touches)
         {
             if(touch.phase == TouchPhase.Began)
             {
                 direction = position;
                 previous_angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
             }
             
              if(touch.phase == TouchPhase.$$anonymous$$oved)
             {        
                 direction = position;
                 angle = $$anonymous$$athf.Atan2(direction.y, direction.x) * $$anonymous$$athf.Rad2Deg;
                 angle_delta = $$anonymous$$athf.DeltaAngle(previous_angle, angle);
                 previous_angle = angle;
             }
         }
             print (angle_delta);
     }
 }




Thank you very much for your help. I still can't seem to be able to get it to work though. When I print the angle_delta, it just prints 0.

avatar image TheEngineer · Feb 05, 2014 at 08:52 PM 0
Share

By the way, I'm using screen space and the bottom left corner is my origin position, A$$anonymous$$A (0, 0).

avatar image TheEngineer · Feb 05, 2014 at 08:55 PM 0
Share

Wow I'm stupid... I just realized I can't use Input.GetTouch(0).position outside of a function... I got it to work. Thanks so much for your help!

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to clamp Lookat? 2 Answers

Rotate player to LookAt a touch position 2 Answers

Make object only rotate in 90 degree increments 0 Answers

2D Torque rotation - stop at certain angle 0 Answers

Rotating Joystick on Touch 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges