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 Leroterr · Oct 24, 2014 at 04:37 AM · 2drotationmovementtransformrotate

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D)

Top-Down 2D:

How can I rotate my player to look at the direction my Joystick is pointing to?

Here is the code that I'm using for my Joystick. It moves my tank properly but at the same rotation (as expected, since I have not coded it yet to rotate/look at the position I'm moving towards to)

 Vector3 tempJoyDelta;
 public static Vector3 joyDelta;
 Vector3 touchPosition;
 public Transform centerPos;
 Ray ray;
 int currTouch = 64;
 int sprintMask;
 bool moving;
 float saucerSpeed;
 public GameObject tank;
 
 void Awake () {
 
         speed = 1.0f;
         sprintMask = 1 << 8;
     
     }
 
 void Update () {    
 
         if(Input.touchCount > 0)
         {
             for(int i = 0; i < Input.touchCount; i++)
             {
                 currTouch = i;
                 ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
                 if(Physics.Raycast(ray, Mathf.Infinity, sprintMask))
                 {
                     if(Input.GetTouch(currTouch).phase == TouchPhase.Began && !moving)
                     {
                         moving = true;
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)
                     {
                         moving = false;
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Stationary)
                     {
                         touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, 0)); 
                     }
                 }
             }
         }
         if(moving)
         {
             joyDelta = centerPos.position - touchPosition;
             joyDelta.z = 0;
 
             tank.transform.Translate(((Vector3.forward * joyDelta.y) * speed) * Time.deltaTime);
             tank.transform.Translate(((Vector3.right * joyDelta.x) * speed) * Time.deltaTime);
         }
 
 }

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

3 Replies

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

Answer by Itaros · Oct 24, 2014 at 06:44 AM

Take the vector at which joystick is pointed and use math to get heading:

 float heading = Mathf.Atan2(joyvector.x,joyvector.y);

It is heading in radians which can be used to point the object in right direction like:

 transform.rotation=Quaternion.Euler(0f,0f,heading*Mathf.Rad2Deg);

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 Leroterr · Oct 24, 2014 at 07:36 AM 0
Share

Thanks! It worked perfectly.

avatar image KingKong320 · Apr 19, 2018 at 07:02 AM 1
Share

its working fine but rotation is not smooth,there are jerks on every rotation.Please help

avatar image
3

Answer by KingKong320 · Apr 20, 2018 at 04:18 AM

 if (moveVec.sqrMagnitude > 0.1) {
         angle = Mathf.Atan2 (moveVec.x, moveVec.y) * Mathf.Rad2Deg; 

         transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (new Vector3 (0, 0, -angle)), Time.deltaTime * rotationSpeed);
     }
Comment
Add comment · Show 1 · 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 KingKong320 · Apr 20, 2018 at 04:20 AM 0
Share

i have got from somewhere it can be achieve like this. Thankyou @InvisionaryU$$anonymous$$ :)

avatar image
0

Answer by InvisionaryUK · Apr 19, 2018 at 11:15 AM

@meticodetest Just use interpolation between angles this should smooth it out for you. il get an example up as soon as i can

Comment
Add comment · Show 1 · 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 buzzing_jelly · May 02, 2020 at 04:17 PM 0
Share

hi, can you explain how to smooth rotation, please?

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

32 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

Related Questions

Find left joystick Vector2 away from sprite's up direction. 1 Answer

Updating rotation of client not working - strange error 0 Answers

Character rotation and move to mouse click point with speed 0 Answers

Problem when rotating sprite and moving it foward 1 Answer

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