Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by JoelAtMoodkie · Apr 24, 2010 at 09:52 PM · vector3rotateoptimizationlookrotationtowards

Can you suggest a faster way to rotate an object?

Hiya all,

I've made a C# function that rotates an object around the Y-axis towards a desired position, with a desired turning speed. At the moment it looks very messy and involves a large amount of resource-heavy functions.

Question: How could I make it faster? (Other than putting it in a co-routine, which I've already done :)).

public void rotateToFace(Vector3 facing, turnSpeed)
{
    // If already facing in direction then return.
    if(facing != new Vector3(0,0,0))
    {
        Quaternion targetRotation = Quaternion.LookRotation(facing);
        float str = Mathf.Min (properties.turnSpeed * Time.deltaTime, 1);
        Quaternion tempRotation = Quaternion.Lerp (transform.rotation, 
                                                    targetRotation, str);
        transform.rotation = Quaternion.Euler(0, tempRotation.eulerAngles.y, 0);
    }
}

I wouldn't say that it is particularly slow at the moment, but I'm sure it could be simplified or done differently, and even saving a handful of clock-cycles is helpful when developing for the iPhone.

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 duck · Apr 24, 2010 at 10:37 PM

First, if you don't know whether this part of your game is actually a significant bottleneck, it probably isn't. Before you get down to optimisation at this level, you really need to make sure you understand how to identify what even needs optimising, otherwise you are quite likely to be just wasting your time.

Going on the assumption that this actually needs optimising, it seems as though you only want it to rotate around the Y axis towards the "facing" direction.

If I've understood the intent of your original code, it might be slightly faster done like this:

Quaternion targetRotation = Quaternion.identity; float turnSpeed = 10;

public void rotateToFace(Vector3 facing, turnSpeed) { // assign new target rotation // (call only when a new direction assigned) Vector3 direction = new Vector3(facing.x, 0, facing.z); Quaternion targetRotation = Quaternion.LookRotation(direction); this.turnSpeed = turnSpeed; }

void Update() {

 float i = turnSpeed * Time.deltaTime;
 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, i);

}

Notes: You don't need to use 'min' to check your 'str' var. Lerp/Slerp auto clamps that parameter to the 0-1 range already. Also it's probably faster to zero the 'y' value of the target vector, rather than use euler angles to do the conversion later.

If your targets are static, and you only need to call "RotateToFace" infrequently, you might notice an improvement. If your targets are in motion, and you need the object to track the target, there may be no noticable improvement at all.

Comment
Add comment · Show 1 · 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 JoelAtMoodkie · Apr 25, 2010 at 12:01 AM 0
Share

Thanks for the suggestions; I completely forgot you could strip the Y-axis that way. The reason I was asking for other peoples opinion was that the algorithm was actually taken from http://forum.unity3d.com/viewtopic.php?t=1237, and was originally a suggestion by a Unity staff member. It didn't seem too concise to me compared to other methods I've seen on the internet.

And any improvement is welcomed here at the moment because lots of little optimizations might add up to a big difference when you've got a shed-load of AI objects interacting.

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

No one has followed this question yet.

Related Questions

unknown axis of rotation 1 Answer

Rotate Rigidbody Towards Velocity? [3d] 0 Answers

How can I rotate my player head to face a target but only when the target is in the front of the player ? 1 Answer

LookRotation and Raycasting 2 Answers

How do I make gameObject.transform.rotation.z equal to a set float value? 2 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