• 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
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
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;

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

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

Related Questions

How do I make a sprite animation rotate towards the cursor? 0 Answers

first person controller 0 Answers

Animation transform lock 2 Answers

animation 360 rotation 1 Answer

Animator Boolean not changing to true in C#, 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges