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 jonathan.sant · Sep 25, 2011 at 04:29 PM · rotationtransformrotatearoundrotatingtransform.rotatearound

Resetting transform's original position and rotation after using transform.rotateAround

enter code hereI have written a script which tilts the player's model (independently of the player's transfrom around a point using) using transform.rotateAround().

What's missing is that I want, at a certain point, to set back the model where it was in the beginning (but relative to the player's transform). I thought this was pretty simple but setting:

 model.transform.rotation = Quaternion.Euler(...)
 model.transform.position = transform.position(...) + new Vector3(...)

is behaving strangely, any ideas on how to reset back the position after using transform.RotateAround() ?

Help would be greatly appreciated, thanks in advance.

===================================================== Edit

Below I've pasted my code:

private void HandleTilting() { // print(model.transform.localEulerAngles.x + ", " + model.transform.localEulerAngles.y + ", " + model.transform.localEulerAngles.z); //print(originalModPos);

     turn = currModelRotY - prevModelRotY;

     if (turn > 0 && model.transform.localEulerAngles.z < 10) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 359);
     if (turn < 0 && model.transform.localEulerAngles.z > 350) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 1);

     //right titling
     if (turn < 0)
     {
         rightAngle = 2 - ((2 * model.transform.localEulerAngles.z) / 40);
         model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, rightAngle);
     }
     else if (turn > 0)
     {
         leftAngle = 2 - (2 * (360 - model.transform.localEulerAngles.z)) / 40;
         model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, -leftAngle);
     }

     if (turn == 0 && model.transform.localEulerAngles.z <= 90 && model.transform.localEulerAngles.z >= 0)
     {
         rightAngle = (2 * model.transform.localEulerAngles.z) / 40;
         model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, -rightAngle);
     }
     else if (turn == 0 && model.transform.localEulerAngles.z >= 270 && model.transform.localEulerAngles.z <= 360)
     {
         leftAngle = (2 * (360 - model.transform.localEulerAngles.z)) / 40;
         model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, leftAngle);
     }
 }

It has to be refined but tilting left when the button is pressed and going smoothly back works perfectly, and so does vice versa but when combining them together ...

well I know what the problem is I just need to set the local z rotation to 360 when tilting left and 0 when tilting right but for some reason the following code doesn't set the rotation

    if (turn > 0 && model.transform.localEulerAngles.z < 10) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 359);
    if (turn < 0 && model.transform.localEulerAngles.z > 350) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 1);

================

Thanks for the swift reply btw.

Comment
Add comment · Show 1
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 · Sep 26, 2011 at 01:26 AM 0
Share

I edited my answer to include a possible solution for what you're trying to do - take a look at that.

1 Reply

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

Answer by aldonaletto · Sep 25, 2011 at 04:42 PM

You must first save the localPosition and localRotation of the model, do the rotation or whatever you want, then restore these local settings. If you just assign them, the model will be "teleported" to the original position/rotation; if you want it to smoothly return to the original settings, you can use the "Lerp filter":

// save the original settings (assuming model is the model transform)
    var rot0 = model.localRotation;
    var pos0 = model.localPosition;
    // do the movement/rotation as you're already doing
    // then restore them using Lerp (assuming this is in Update):
    var dt = Time.deltaTime * 2.3; // 2 to 5 give reasonable results
    model.localRotation = Quaternion.Slerp(model.localRotation, rot0, dt);
    model.localPosition = Vector3.Lerp(model.localPosition, pos0, dt);
The actual implementation depends on your original script. If you can't find the best way to match this code to your script, post the script here and we'll try to help you.

EDITED: From your script, I suppose you want to "bank" your model based on a pivot 5 units above the center. The easiest way is to do that is to have an intermediate empty object (let's call it Pivot) with local position = (0,5,0). The model is childed to the Pivot, and the Pivot is childed to your character, like this:

Character
  Pivot
    Model
When banking to any side, just set the Pivot localEulerAngles like this:

    Pivot.localEulerAngles = Vector3(0, 0, angle);
It's better to set the localEulerAngles as a whole, instead of just setting its z component - setting individual components causes small drifts that may accumulate and let the object in a weird position after some time.
All Rotate functions (Rotate, RotateAround) have the same problem: you loose the angle information, because you just can't trust in the returned euler angles! The 359/-1 uncertainty is just the tip of the iceberg: after some rotations, the craziest angle combinations may be returned, and you have no chance to correctly interpret them.
For this reason, always avoid reading euler angles; if you must rotate something and still know the euler angle at any time, do the inverse thing: keep the angle in a variable, increment or decrement it (always modulo 360) and set the euler angles at once using a Vector3 structure - like in this generic Rotate function:

var xAngle: float = 0; var yAngle: float = 0; var zAngle: float = 0;

function RotatePrecisely(angX: float, angY: float, angZ: float){ xAngle = (xAngle + angX) % 360; yAngle = (yAngle + angY) % 360; zAngle = (zAngle + angZ) % 360; transform.eulerAngles = Vector3(xAngle, yAngle, zAngle); } The precise x, y and z angles are available at the variables xAngle, yAngle and zAngle.

Comment
Add comment · Show 5 · 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 jonathan.sant · Sep 25, 2011 at 05:24 PM 0
Share

I've edit the problem.

avatar image jonathan.sant · Oct 02, 2011 at 01:50 PM 0
Share

thx this was awesome

avatar image brett nieland · Aug 30, 2013 at 04:50 PM 0
Share

Thanks! I used this to manage tilting the camera location in response to user input.

avatar image Lorcan · Aug 30, 2013 at 05:27 PM 0
Share

Just so we're on the same page, is this a way to reset the camera to a specific rotation on button press after rotating the camera around the player? I've been trying to figure that out and made my own question for this issue.

avatar image brett nieland · Aug 30, 2013 at 08:48 PM 0
Share

@Lorcan : I found your Q and answered it there!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate around moves object out of position, why? 1 Answer

Rotating a camera around a sphere to look at a specific point 2 Answers

How to limit the angle of a camera with transform.RotateAround? 1 Answer

Get the rotation of a object around a arbitrary axis. 0 Answers

Rotate Around Planet with Spaceship Face Set on Path 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