• 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 jdonaldsonsch · Jul 16, 2018 at 11:56 PM · animationrotationanimator controllermouselookflip

Image Flip/Rotations with the mouse above/below 90/-90 degrees to the point. How do I accomplish this?

Hello everyone, I have been stuck on a problem with image rotation/flipping based on mouse position tracking. the 2 videos below depict what I want the rotation/flip to look like, then what it currently is doing. The first video is showing a simple code I constructed using Game Maker. I am relatively new to Unity and have been trying to solve this issue for a while. None of the scripts I have found thus far have helped. The Second image is what I have been able to do in Unity. It is able to track the mouse's position and rotate towards it using the following script.

Goal - https://youtu.be/0rCbQtusL9s Current - https://youtu.be/7qv5oFHF9Rs

What should i use to alter the direction in which the image is facing? Or better yet, how do i flip the entire player to always face towards the mouse position and rotate the weapon towards the mouse target while still maintaining the desired animations? This is a loaded question and again, i apologize for being new. I'm learning and I am trying to solve this issue, but I could really use some help. Thank you for your time! !

 //Variables for positions
     public Vector3 mousePosition;
     public Vector3 gunPos;
     public float angle;
 
  void Update()
     {
         faceMouse();
     }
 
     void faceMouse()
     {
         Vector3 mousePosition = Input.mousePosition;
         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
 
         Vector2 direction = new Vector2(
             mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
         transform.right = direction;
 
     }

Comment
Add comment · Show 2
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 NoDumbQuestion · Jul 17, 2018 at 09:54 AM 1
Share

You should inverse localRotation on z axis if your direction.z axis is bigger or smaller than 0

avatar image jdonaldsonsch NoDumbQuestion · Jul 17, 2018 at 09:07 PM 0
Share

Thank you for the suggestion and I apologize for my inability to fully understand it. From what I can understand, direction.z (if you are referring to the Vector2 direction) is not a usable number since its a 2D object. Can I ask you to help me by providing an example as to what you are suggesting might look like?

P.S. thank you so much

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jdonaldsonsch · Jul 19, 2018 at 04:40 AM

So i have tried coding it with a similar formula to what you placed however I am having difficulty with the transform.localRotation.z lines. It states that it "Cannot modify the return value of 'Transform.localRotation' because it is not a variable". Again since I'm trying to learn I have attempted a few fixes, none of which have worked. I understand what your code is trying to do, I don't understand however, why the engine is unable to recognize the transform component and carry out the rotation. It being in a void should not require a return value to modify or a variable to assign the value to. Do you have any further suggestions as to how to fix this?

Again, i appreciate you assisting me with this.

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 NoDumbQuestion · Jul 19, 2018 at 04:45 AM 0
Share

I forgot about transform localRotate is not a variable.

You have to make a new vector to replace localRotate. You cannot change localRotate.z because that is how unity design to stop people make a race condition or bugs in c++ I guess.

 var copyVector = transform.localRotation;
 
 copyVector.z = 180;
 
 transform.localRotation = copyVector;
avatar image
1

Answer by NoDumbQuestion · Jul 18, 2018 at 01:46 AM

Z-axis in 2D is for flipping image. Like in mirror if you have z-xis = 180 or -180.

It would be something like this.

     void faceMouse()
     {
         Vector3 mousePosition = Input.mousePosition;
         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
         if (mousePosition.x > transform.position.x) // Mouse on the left of character
             //Flip image normal
             transform.localRotation.z = 0;
         else // Mouse on the right of character
             //Flip image 180 in z axis
             transform.localRotation.z = -180;
         Vector2 direction = new Vector2(
         mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
         transform.right = direction;     
     }
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

I has a problem when i pressed "M" key animation not play. I mean amination play but not properly. Animation play properly when i hold "M" key? please help me. I want to play animation on single just click. 0 Answers

How to set animations for a top down shooter? How to change the animations by the player rotation? 0 Answers

I can't see or access a state in the Animation Controler 1 Answer

Animation from child object overrided by its parent and won't show in game window 0 Answers

Ragdoll death respawn help 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