Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by TriBolt72 · Nov 30, 2015 at 02:54 AM · 2drotationmovementtouchtouch controls

Trying to get the player to face the direction of a touch

I'm having quite the issue with player movement, and I've searched for answers but had no luck. So the I have a ship and the ship follows my finger. That works. What I cant seem to get is the ship to rotate and face the direction of the touch. Pretty much all the examples I've seen for doing this have been with Mouse input or in 3d games, but I have a touch (and drag) top down 2d game. The top of my ship is what I want to spin around to face my finger but I havent gotten anywhere trying to get a solution. Here is what I have for movement so far...

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     
     public float speed = 1f;
     Vector2 pos;
 
     void Start ()
     {
 
         pos = transform.position;
 
     }
 
     void Update ()
     {
         pos = transform.position;
 
         if (Input.touchCount > 0) {
 
             Vector2 touchPos = Input.GetTouch (0).position;
 
             transform.position = Vector2.MoveTowards (pos, Camera.main.ScreenToWorldPoint (touchPos), speed * Time.deltaTime);
     
 
         }

} Any help is appreciated. I'm fairly new to this, so you may have to elaborate... Thanks!

Comment
Add comment
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

1 Reply

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

Answer by Aggrojag · Nov 30, 2015 at 04:02 AM

  Vector2 dir = touchPos -  transform.position;
  float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Source(http://answers.unity3d.com/questions/654222/make-sprite-look-at-vector2-in-unity-2d-1.html)

Note from robertbu (source): Note this code assumes that the 'forward' side of your sprite is the right side. If it is another side (like up), you will need to adjust the angle (subtract 90) before passing it to the AngleAxis() function;

The touch position is in place of the click on this script.

You have to use some basic trigonometry to figure out what direction face. Trig is typically taught using triangles, but it deals with circles at it's most fundamental. I'm assuming you've heard of the Unit Circle (essentially a circle with a radius of 1 at 0,0). Your touch point is the distance from the origin. The x,y coordinate is essentially the position on this circle. Atan2(arc tangent) takes the distance, in accordance with both x and y position, and returns the associated angle being swept counter-clockwise from (1, 0) on the unit circle that intersects with this position. This is returned in radians, which is why you multiply by Rad2Deg(conversion rate).

I probably missed something, or was slightly off on some bit of information. This should be enough to get you started as this is all from memory and code that I grabbed from said source to save me some time.

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 TriBolt72 · Nov 30, 2015 at 09:03 PM 0
Share

Thanks so much for the help! This script works really well but I have one more question. How would I go about making this rotate at a certain speed. Right now to points directly at my finger, but can I make it spin to face my finger not so...automatically i guess?

avatar image Aggrojag TriBolt72 · Nov 30, 2015 at 09:13 PM 0
Share

You would treat the angle variable as where you end up facing. Then you would figure out if you need to rotate left or right (shortest distance). From there you would increase/decrease your current angle until it's within some acceptable range (say +-.5f[if(curAngle >= angle -.5f && curAngle <= angle +.5f)]) that way it doesn't have to be exact, as most times you'll find dealing in exact numbers is bad when it comes to float. Then you're done. Probably wont post an example script as this should be doable at your level of understanding (though possibly difficult). Just break it down in to easy to accomplish bits of code, and you'll do fine. Cheers.

avatar image TriBolt72 Aggrojag · Dec 01, 2015 at 03:53 AM 1
Share

I'll try this tomorrow then post back with what I can get. Thanks for all of your help!

Show more comments

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

50 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d Movement Messes up when cube is rotatated 0 Answers

different devices -> Different touch speed 3 Answers

I Need Help With Mobile Touch Movement. 0 Answers

Basic 2D movement C# - Key presses cancel eachother out 4 Answers

Rotating an object towards target on a single axis 2 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