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
0
Question by Leafenzo · Feb 09, 2019 at 06:01 AM · rotationeuleranglesrotatearound

trying to RotateAround an object around another object using mouse

This is almost working. I just can't figure convert the angle given from rotZ in the RotateAround. What it seems to be going wrong is that it's not using rotZ as an angle to rotate to, and instead takes it as a speed to rotate at. How do I make it rotate to that number as an angle instead of taking it as a number to increase its angle by?

 private Vector3 zAxis = new Vector3(0, 0, 1);
     public Camera cam;
 
     void Update()
     {
         Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.parent.transform.position;
         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         
 
         transform.RotateAround
             (
             this.transform.parent.transform.position,
             zAxis,
             rotZ 
             );
     }
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 Leafenzo · Feb 09, 2019 at 07:41 PM

I found a workaround that may only really work for my exact situation, I take a couple specific angles then place the transform I'm trying to move a distance from the center along those angles.

I did receive help for a majority of this code from another user on this forum.

Original code just for drawing some gizmo lines on angles:

 private void OnDrawGizmos()
     {
         // / Special thanks Ymrasu
         Gizmos.color = Color.yellow;
 
         float length = 10f;
         var startPoint = transform.position;
 
 
         float angle1 = ((rotZone * 45) + 90 + 22.5f) * Mathf.Deg2Rad;
         float angle2 = ((rotZone * 45) + 90 - 22.5f) * Mathf.Deg2Rad;
         var endPoint1 = new Vector2(startPoint.x + length * Mathf.Cos(angle1), startPoint.y + length * Mathf.Sin(angle1));
         var endPoint2 = new Vector2(startPoint.x + length * Mathf.Cos(angle2), startPoint.y + length * Mathf.Sin(angle2));
 
         Gizmos.DrawLine(startPoint, endPoint1);
         Gizmos.DrawLine(startPoint, endPoint2);
     }

Reused to place a transform on the end of a line

     public Transform atkPos;
     public float distance = 1f;
     public float Rotoffset;
     private void Update ()
     {
         ///desRotA = transform.rotation.z;
         ///atkPos.RotateAround(this.transform.position, zAxis, Mathf.Abs(90 - desRotA));
 
         float angle = ((rotZone * 45) + 90) * Mathf.Deg2Rad;
         var startPoint = transform.position;
         var endPoint = new Vector2(startPoint.x + distance * Mathf.Cos(angle), startPoint.y + distance * Mathf.Sin(angle));
         atkPos.position = endPoint;
 
         Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + Rotoffset);
         findRotationzone();
     }

I'm not marking this as the answer though, as this only works for 8-degree rotation (Although It wouldn't be too hard to make it work for 360.), as it doesn't use RotateAround, and as it does not rotate the moved transform with it's rotation around an object. But this might still be helpfull sometime though.

Comment
Add comment · Show 3 · 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 sean244 · Feb 09, 2019 at 10:49 PM 1
Share

Sorry if I misunderstood your question, but you're not just trying to get your object to rotate towards the mouse, are you?alt text

avatar image Leafenzo sean244 · Feb 09, 2019 at 11:58 PM 0
Share

Sorry If I phrased it confusedly. I am not trying to get my object to rotate towards the mouse, I'm trying to rotate it around another object to face the mouse compared to the other object. Using the RotateAround function, as in it rotates around another object.

avatar image sean244 Leafenzo · Feb 10, 2019 at 01:04 AM 0
Share

Ah, gotcha :) Well I'm happy you found a solution :)

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

134 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

Related Questions

Proper way to rotate an object? 1 Answer

RotateAround reversing doesn't work with negative vector3 0 Answers

Rotation Problem - Simulating Gimbal lock 1 Answer

Rotation promlems past 90 and 270 3 Answers

Rotation not observing limits 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