Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by AndersNOR · Feb 10, 2016 at 12:00 PM · sprite2d-platformerrotate objectmouseposition

Making a sprite rotate towards mouse when sprite is not centered on screen

Hello. I have been looking through the forums but not found anything that quite solves my problem.

I have a sprite that is rotating around the main character. The mouse position is changing its rotation so it always points towards the mouse. This works all fine and dandy but heres the problem:

It only works smoothly when the player is centered on the screen. I want it to always point towards the mouse even if the player is not centered.

Here is the code I have for handling the rotation:

     Vector2 target = new Vector2 (shieldCenter.transform.position.x, shieldCenter.transform.position.y); //vector for transforming the shield to the target, aka the player
     Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,10); //where the mouse is on the screen
     Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos); //position for the shield to look towards
     lookPos = lookPos - shield.transform.position; //make the shield look towards the mouse
     angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg; //turn radians to degrees
     shield.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); //transform the rotation of the shield towards the mouse
     shield.transform.position = target; //set the position of the shield to the target


the shields pivot is centered while the sprite itself is drawn on the right side of it. Which allows it to rotate around the player. The shields pivot is set to the center of the player by code.

Here's a screenshot of my game so you might have a better idea of what I am trying to achieve:

alt text

On this example the player is located on the left side of the screen. Which messes up the rotation if i am trying to rotate the shield on the players left side. If the camera was centered on the player it would have worked perfectly.

skjermbilde-2016-02-10-123937.jpg (523.9 kB)
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
0

Answer by Dr-Orange · Nov 13, 2016 at 11:46 AM

Make an empty game object then put it on the centre of the character. call it "axis" attach the script below to it then get your shield component and attach it and a child of your axis.

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

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.

Follow this Question

Answers Answers and Comments

45 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

Related Questions

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

Sprite Editor/Pack Atlas Question for 2D Animating 0 Answers

Top Down Sprite Facing Mouse 2 Answers

Getting mouse coordinates on sprite 1 Answer

Rotate object based on mouse position 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