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 /
  • Help Room /
avatar image
0
Question by Spudnik-EX · Feb 22, 2019 at 07:07 AM · sprite2d gamemousequaternion2d sprites

Sprite Orbiting Sprite with Mouse

I've had this issue for a while now, and I've tried different solutions but i cannot figure this out. I have a 2D game which i have two sprites. The first sprite is the player object and the second is an orb that is a child of the first, so when the player moves, the orb moves. I would like to have the orb rotate around the player according to where the mouse position is related to the player. Billboarding is not an option (or at least one I've found) because I have several other scripts set up which cause complications.

The first solution I tried is this:

 public class LimbSlide2 : MonoBehaviour
 {
     public Camera mainCamera;
     public Transform pivot;
     public Transform orb;
     public float radius;
 
 
 // Use this for initialization
 void Start()
 {
 
     pivot = this.transform;
     //transform.position += Vector3.up * radius;
 
 }
 
 // Update is called once per frame
 void Update()
 {
     Vector3 orbVector = Camera.main.WorldToScreenPoint(orb.position);
     orbVector = Input.mousePosition - orbVector;
 
     float angle = Mathf.Atan2(orbVector.y, orbVector.x) * Mathf.Rad2Deg;
 
     pivot.position = orb.position;
 
     pivot.rotation = Quaternion.AngleAxis(angle - 90, Vector3.up);
     //or
     //transform.rotation = Quaternion.AngleAxis(angle-90,Vector3.up);
 }

which needs to be attached to a parent object and affects the orbs but also affects the parent (in this case the player) and resulted in this: https://imgur.com/a/gAcX0tm. The issue with this is it rotates the player sprite as well as the orb sprite, flattening them out and then expanding again.

​

Solution 2 was closer, however I am not able to control the direction of the rotation with the mouse, it just rotates at a constant speed.

 public class Target : MonoBehaviour
 {
     public float Speed;
     private float _angle;
     public Transform orb;
 
     public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Quaternion angle)
     {
     return angle * (point - pivot) + pivot;
     }
 
 
 private void Start()
 {
 
 }
 
 private void Update()
 {
     Vector3 orbVector = Camera.main.WorldToScreenPoint(orb.position);
     orbVector = Input.mousePosition - orbVector;
     float _angle = Mathf.Atan2(orbVector.y, orbVector.x)* Mathf.Rad2Deg;
     transform.position = RotatePointAroundPivot(transform.position, transform.parent.position, Quaternion.Euler(0,_angle,0));
 
 }
 
 
 
 }

Resulting in: https://imgur.com/a/bGlkR8I Which is closer to what I need because both sprites are facing the camera, however I can't control the rotation with the mouse. Any ideas?

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

0 Replies

· Add your reply
  • Sort: 

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

217 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

Related Questions

How do I outline sprites hidden behind another sprite? 0 Answers

How to make specific 2D-sprites in unity, Trouble with sprites 0 Answers

[Help] Merging multiple sprites to create a long single background 0 Answers

Extend arrow sprite 0 Answers

Sprite shaking/ jittering/ jiggling/ wiggling/ vibrating when moving it using joystick (2D). 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