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 Taxen0 · May 27, 2014 at 01:07 PM · rotationraycastspritemouselook

2D Rotate sprite (arm) so it faces the mouse.

Hello,

I found several answers to this question but they don't seem to work for me. I want to find one that actually works before I dig into the code and see how it is put together.

I have a sidescrolling game and i want to rotate the arm of my character so it points towards the mouse.

I tried several solutions and the best one was working while my character was facing right but the controls were inverted when facing left, i guessthis is because i simply flip the X of the sprite when it turns.

I started looking for a way using raycast since this might solve my problem?

I looked at http://www.unifycommunity.com/wiki/index.php?title=LookAtMouse and the script looks promising but nothing happens when I attach it to the object.

Any help with this would be appreciated, and if possible I would prefer a C# solution.

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
3
Best Answer

Answer by zientasek · May 27, 2014 at 09:17 PM

 using UnityEngine;
 using System.Collections;
 
 public class rotateToMouse : MonoBehaviour {
 
 
 
     // Update is called once per frame
     void Update () 
     {
         Vector2 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition);        //Mouse position
         Vector3 objpos = Camera.main.WorldToViewportPoint (transform.position);        //Object position on screen
         Vector2 relobjpos = new Vector2(objpos.x - 0.5f,objpos.y - 0.5f);            //Set coordinates relative to object
         Vector2 relmousepos = new Vector2 (mouse.x - 0.5f,mouse.y - 0.5f) - relobjpos;
         float angle = Vector2.Angle (Vector2.up, relmousepos);    //Angle calculation
         if (relmousepos.x > 0)
             angle = 360-angle;
         Quaternion quat = Quaternion.identity;
         quat.eulerAngles = new Vector3(0,0,angle); //Changing angle
         transform.rotation = quat;
     }
 }


May be not intuitive but it works.

Make sure that when arm is pointing upwards rotation is 0,0,0

Comment
Add comment · Show 6 · 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 Taxen0 · May 27, 2014 at 10:26 PM 0
Share

Thanks, I think this is similar to something I found and tried earlier.

However it suffers from the same issues with the controls getting inverted when my character faces left, I am using the character movement script from the Unity assets pack and it simply flips the image when you turn around.

By inverted I mean that if I move the mouse up 45 degrees it will move down 45 degrees ins$$anonymous$$d.

avatar image Taxen0 · May 29, 2014 at 10:38 AM 0
Share

I just got this working like I wanted, just added a if statement to invert the X-axis of relmousepos if the character is facing the other way.

cheers!

avatar image root8888 · Jun 17, 2014 at 04:10 AM 0
Share

This works almost perfectly for me as well, thanks. The only thing that I notice is that the angles are a bit off at some points. For example at 0, 90, 180, 360 degrees, the object dead on points at the mouse. At 45, 135, 225, 315 degrees, it's very noticeably off. Perhaps it is because my game is 16:9 ins$$anonymous$$d of a square? I don't know exactly what is going on in your code yet, so I'm not sure. Any ideas? At 45 degrees, the object points above the mouse and at 135 degrees the object points below the mouse.

avatar image zientasek · Jun 17, 2014 at 09:09 PM 0
Share

I'm glad I helped. I spend over an hour to figure out how to accomplish this. At the end, I noticed that my first attempt was best. And I have no idea why offset is present. I'll try to fix this later.

avatar image Daniel311311 · Aug 29, 2014 at 05:40 AM 0
Share

A little late to the party, but I am having this same issue. the one thing I need to accomplish is to get reverse the x-axis for the mouse when my character is flipped. Taxen0 I was wondering if you could share the line of code you used to do this?

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

23 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

Related Questions

raycast to determine pivot 1 Answer

2D animation : problem with rotation interpolation 1 Answer

2D Sprite Rotation 0 Answers

Pointing object at mouse using raycast only works when object is center screen. 2 Answers

Mathf.Clamp is 'Sticky'? 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