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
4
Question by NikEy · Jul 09, 2013 at 12:29 AM · rotaterotatearound

RotateAround without transform

Hi,

Unfortunately my game cannot rely on transform functions, so I have to rewrite them. So for example for:

 transform.Translate(Vector3.forward * speed);

I use:

 transform.position += Vector3.forward * speed;

How would I be able to apply this to RotateAround? Specifically:

 transform.RotateAround(Vector3.forward, speed / 10f);

I tried things like

 transform.position = Quaternion.Euler(0, 0, 1f) * transform.position; //etc

but that didn't work.. =/

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

2 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by aldonaletto · Jul 09, 2013 at 01:36 AM

I don't know why your game can't use Transform functions, but it can be done with the following code:

 function RotateAround(center: Vector3, axis: Vector3, angle: float){
   var pos: Vector3 = transform.position;
   var rot: Quaternion = Quaternion.AngleAxis(angle, axis); // get the desired rotation
   var dir: Vector3 = pos - center; // find current direction relative to center
   dir = rot * dir; // rotate the direction
   transform.position = center + dir; // define new position
   // rotate object to keep looking at the center:
   var myRot: Quaternion = transform.rotation;
   transform.rotation *= Quaternion.Inverse(myRot) * rot * myRot;
 }

Hope you can at least use Quaternion functions!

NOTE: This function is based on the original Unity code for RotateAround (thanks to ILSpy for that!).

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 aldonaletto · Jul 09, 2013 at 01:52 AM 0
Share

1- Fixed an error at line 6: it should be center + dir ins$$anonymous$$d of pos + dir

2- Fixed error in rotation: again with help of ILSpy, found that the implementation of Rotate isn't that easy!

avatar image NikEy · Jul 09, 2013 at 02:05 AM 2
Share

Worked flawlessly, thanks!

Here the same code in C#:

 void RotateAround(Vector3 center, Vector3 axis, float angle) {
     Vector3 pos = transform.position;
     Quaternion rot = Quaternion.AngleAxis(angle, axis); // get the desired rotation
     Vector3 dir = pos - center; // find current direction relative to center
     dir = rot * dir; // rotate the direction
     transform.position = pos + dir; // define new position
     transform.rotation *= rot; // rotate object to keep looking at the center
 }
avatar image KozakLuke NikEy · Jan 25, 2017 at 09:49 AM 0
Share

Correct code in C#.


private void RotateAround(Vector3 center, Vector3 axis, float angle) {
    Vector3 pos = this.transform.position;
    Quaternion rot = Quaternion.AngleAxis(angle, axis); // get the desired rotation
    Vector3 dir = pos - center; // find current direction relative to center
    dir = rot * dir; // rotate the direction
    this.transform.position = center + dir; // define new position
    // rotate object to keep looking at the center:
    Quaternion myRot = transform.rotation;
    transform.rotation *= Quaternion.Inverse(myRot) * rot * myRot;
}

avatar image aldonaletto · Jul 09, 2013 at 02:15 AM 0
Share

There were two errors in the original script at lines 6 and 7 - take a look at the fixed answer: replace pos with center at line 6, and the object rotation at line 7 must be more complex in order to keep the original orientation relative to the center.

avatar image not_a_valid_username · Apr 28, 2020 at 10:15 PM 1
Share

The last line has an extra quaternion multiplication. It should be: transform.rotation = rot * myRot;

avatar image normus28 not_a_valid_username · Mar 01, 2021 at 01:32 PM 0
Share

I had some weird rotation issues resulting in NaN quaternion, changing last line to transform.rotation = rot * myRot; solved it! Thank you!

avatar image
-2

Answer by JuanseCoello · Jul 09, 2013 at 02:09 AM

You can use this C # script to rotate around with "D" AND "A" or arrows.

using UnityEngine; using System.Collections;

 public float rotateSpeed = 2.0F;
 private Vector3 moveDirection = Vector3.zero;
 
 void Update() {
 
 CharacterController controller = GetComponent<CharacterController>();
 
 {
 
 transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0); // Rotation
 

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

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

Camera Orbit Rotation Problem 0 Answers

transform.rotate only 1 time for 180 degrees? 1 Answer

RotateAround 2 axes at once 0 Answers

How do I rotate an object around another to have its opposite ? 0 Answers

I want to use LookAt but based on the object original rotation 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