• 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 /
  • Help Room /
avatar image
0
Question by DBarnes94 · Mar 04, 2016 at 01:14 PM · c#2drotationtopdown

2D Sprite issue when rotating towards mouse click location.

Hello there, I am fairly new to Unity, but I am experiencing a slight issue when rotating my sprite towards a mouse click location in a 2D top-down scene. The sprite will move towards the location and rotate accordingly, however upon reaching this location, its rotation changes back to its original transform rotation when the scene was started. I have tried a few different rotational methods but all of these generally have the same issue, which leads me to think its a program structure problem. Ideally I would like the sprite to move and rotate to the location clicked or where the mouse is currently being held down, and then to stay at the rotation that it was moving at when it reaches the destination.

Here's my code to look through, any help on the matter would be much appreciated. Thanks.

 using UnityEngine;
 using System.Collections;
 
 public class Cat : MonoBehaviour
 {
     public float speed = 1.5f;
 
     private Vector2 moveLocation;
     private Vector3 mouseRotation;
     private Vector3 mousePosition;
 
     private Vector3 newPos;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0)||Input.GetMouseButton(0))
         {
             //Save the position of the location clicked
             mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             moveLocation = new Vector2(mousePosition.x, mousePosition.y);
 
             //Save the rotation of the mouse relative to the z-axis of Cat
             mouseRotation = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - transform.position.z));
         }
 
         //Update the Cat's position based on the mouse click location
         transform.position = Vector2.MoveTowards(transform.position, moveLocation, speed * Time.deltaTime);
 
         //Update the Cat's rotation based on mouse click location
         transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePosition - transform.position);
         //transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * Mathf.Rad2Deg - 90);
         //Quaternion.AngleAxis(Mathf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * Mathf.Rad2Deg - 90, Vector3.forward);
         //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, Mathf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * Mathf.Rad2Deg - 90), 2.0f * Time.deltaTime);
     }
 }
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 DBarnes94 · Mar 10, 2016 at 02:48 AM 0
Share

It may seem slightly messy with the coding but this seems to have solved the problem, a big thanks to Salocin19 for pointing out the issue and providing a solution to the problem.

See below for the code used for anyone else experiencing a similar problem.

         //Check for position and mouse click location meeting so that we don't rotate unnecessarily
         if ((transform.position.x != mousePosition.y) && (transform.position.y != mousePosition.y))
         {
             //Update the Cat's rotation based on mouse click location
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, $$anonymous$$athf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * $$anonymous$$athf.Rad2Deg - 90), 2.0f * Time.deltaTime);
         }

2 Replies

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

Answer by Salocin19 · Mar 05, 2016 at 02:50 AM

I believe the problem here is the following scenario:

-Cat has reached mouse location

-Cat then tries to update its rotation with :

      transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePosition - transform.position);*


-BUT, at this point, the cat's position and the mouse's position are the same.
-Therefore, the rotation will be set to 0 because mousePosition - transform.position = 0.

One possible way to fix this:

-Before updating the rotation, check to see if the Cat's position and the mouse position are the same. If so, go ahead and update rotation. Otherwise, don't.

Hope this helps!

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

Answer by DBarnes94 · Mar 05, 2016 at 03:49 PM

Brilliant that could very well be it, i'll take a crack at it the next chance that I get and let you know if it does indeed work. Thanks.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Freeze rotation X but rotation Z, how can I do this? 0 Answers

Is there any way to trigger a popUp UI when a player steps in a specific set of tiles? 0 Answers

can't figure out top down 2D rotation (noob) 0 Answers

Transition between two angled planes 1 Answer

After Rotating a Prefab, Transform.Position of children is inaccurate 1 Answer


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