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
0
Question by MrProfessorTroll · Dec 14, 2013 at 04:00 AM · quaternionmovetiltroation

Tilt based on direction.

Hey, I have a ship that I would like to be tilted as it moves. I have tried numerous approaches such as: void playerMovement() { cameraDifference = mainCam.transform.position.y - myTransform.position.y;

         Vector3 movementDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
         Vector3 lookDirection = new Vector3(Input.mousePosition.x, Input.mousePosition.y, cameraDifference);
 
         lookDirection = mainCam.ScreenToWorldPoint(lookDirection);
         myTransform.LookAt(lookDirection);
 
         movementDirection *= playerSpeed;
 
         if(myTransform.rotation.eulerAngles.y < 45f && myTransform.rotation.eulerAngles.y > -45f)
         {
             myTransform.rotation = Quaternion.Euler(movementDirection.z * 3f, myTransform.rotation.eulerAngles.y, -movementDirection.x * 2f);
         }
         else if(myTransform.rotation.eulerAngles.y >= 45 && myTransform.rotation.eulerAngles.y < 90f)
         {
             myTransform.rotation = Quaternion.Euler(movementDirection.x * 3f, myTransform.rotation.eulerAngles.y, -movementDirection.z * 2f);
         }
 
         player.SimpleMove(movementDirection);
     }

This works but it is very sloppy. So I decided to develop a new method that would work better. This method is confusing for me though as I do not know how to implement it. Here is the method - Based on the movement of the player, the ship will tilt in the direction of the movement. I know how to get the direction but I do not know how to apply this direction as a rotation. So if the player is facing north and moving north, the tilt needs to be north and etc.

(I do not want the player to look at the movement direction, but I want it to tilt towards the movement direction)

Thanks

EDIT:

Here is a picture of what I mean:

alt text

REMEMBER: I am making the player face the mouse position. The Y axis is always rotating, which means that I cannot base my rotation only if I am facing forward.

picture.jpg (78.8 kB)
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
2
Best Answer

Answer by sparkzbarca · Dec 14, 2013 at 04:24 AM

should be easy enough.

what you need is just to know the axis to rotate on to tilt.

for reasons that have to do with math

axis of rotation =

vector3.cross(DirectionYourGoing.normalized, Ship.transform.up)

now rotate around that axis say 30 degrees and you should be good.

basically cross does this

take a piece of paper and have one edge going towards up (the opposite of course going down)

now the other 2 edges are pointing towards your direction of movement. cross returns the vector pointing directly in front of the paper.

alternatly if you assume a flat piece of paper/terrain. Cross returns the vector pointing straight up from the ground.

The reason this is useful is because that's the axis thats 90 degrees from your movement axis so it's the one you want to rotate on.

so use this roughly

 void OnStopMove()
 {
 //undo rotation
 playership.transform.rotate(Axis,-angle);
 }
 void OnMove()
 {
 float Angle = 30f;
 vector3 Axis = vector3.cross(MoveDirection.normalized, PlayerShip.transform.up);
 Playership.transform.Rotate(Axis, Angle);
 }
 
 Void Update()
 {
 if(//movekeydown())
 {
 Movedirection =....;
 OnMove();
 }
 if(//movekeyup())
 {
 OnStopMove();
 }
 if(movekey)
 {
 //move
 }
 }
 
 this will make it so when you press the key the first moment you press it, it rotates, then while you hold it down you move, then when you let it up it stops moving and unrotates
Comment
Add comment · Show 10 · 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 Sospitas · Dec 14, 2013 at 04:29 AM 0
Share

Hmmm, yeah, I do prefer your answer to $$anonymous$$e now that I think about it. Accounts for different axis alignments without having to change the code.

And sideways movement rotation is just PlayerShip.transform.Rotate($$anonymous$$oveDirection.normalized, Angle);

avatar image MrProfessorTroll · Dec 14, 2013 at 04:32 AM 0
Share

It does not work. I have tried it but you have to remember, I am making the player face my mouse position so his forward is always changing. I will try to upload a video of my problem so you can see.

avatar image MrProfessorTroll · Dec 14, 2013 at 04:43 AM 0
Share

Here is a video I recorded of my problem. http://www.youtube.com/watch?v=IJg2aPl8RzQ

avatar image Sospitas · Dec 14, 2013 at 05:15 AM 1
Share

To smooth rotations out you use Quaternion.Slerp

An example of this can be seen in the second example shown here

In terms of yours, you have the current rotation. The target rotation will be something like:

 Quaternion targetRotation = myTransform.rotation * Quaternion.AngleAxis(angle, axis);

Then do

 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, slerpSpeedValue);

Where slerpSpeedValue is a float that you can change to speed up/slow down the rotation speed.

avatar image Sospitas · Dec 14, 2013 at 05:27 AM 1
Share

Edited it now. I was being stupid, it's multiplication not addition. $$anonymous$$y bad

Show more comments
avatar image
0

Answer by Marrt · Apr 24, 2018 at 09:42 PM

If anyone wants a more potent solution:

  • that automatically scales up the tilt with the magnitude of the velocity

  • never overshoots input rotation by more than 90 degree

  • can be scaled easily by specifying the magnitude that should lead a 45 degree offset to input rotation

The formatting on this page is horrendous, feel free to fix it, i failed

 /// <summary>
     /// Tilts a rotation towards a velocity relative to referenceUp
     /// Example:
     /// myTransform.rotation = TiltRotationTowardsVelocity( myCleanRotation.rotation, Vector3.up, velocity, 20F );
     /// </summary>
     /// <param name="cleanRotation"        >Target rotation of the transform, maybe your transform is already looking at something, you don't want to loose this alignment</param>
     /// <param name="referenceUp"        >The up Vector, mostly, this will be Vector3.up, if your gravity is pointing down</param>
     /// <param name="vel"                >The velocity vector that is meant to cause the tilt</param>
     /// <param name="velMagFor45Degree"    >A velocity with a magnitude of velMagFor45Degree will yield a 45degree tilt</param>
     /// <returns>returns currentRotation modified by a tilt</returns>
     public static Quaternion TiltRotationTowardsVelocity( Quaternion cleanRotation, Vector3 referenceUp, Vector3 vel, float velMagFor45Degree )
     {
         Vector3 rotAxis        = Vector3.Cross( referenceUp, vel );
         float    tiltAngle    = Mathf.Atan( vel.magnitude /velMagFor45Degree) *Mathf.Rad2Deg;
 
         //    Helping tp visualize, https://docs.unity3d.com/ScriptReference/Vector3.Cross.html
             //    Debug.DrawRay( myTransform.position, referenceUp,    Color.yellow    );    //leftHand thumb
             //    Debug.DrawRay( myTransform.position, vel,            Color.green        );    //leftHand index
             //    Debug.DrawRay( myTransform.position, rotAxis,        Color.cyan        );    //leftHand middle
 
         return Quaternion.AngleAxis( tiltAngle, rotAxis ) *cleanRotation;    //order matters
     }









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 wafflesmart · Dec 24, 2018 at 04:50 AM 0
Share

What does the cleanRotation variable represent? I'm using transform.Rotate for that parameter, but the only rotation I get is around the z axis when I move along the x.

eg:

transform.rotation = TiltRotationTowardsVelocity( transform.rotation, Vector3.up, rigid.velocity, 20F );

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

20 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

Related Questions

how to rotate an object smoothly 2 Answers

Rotating an object only a set amount of degrees 1 Answer

Top down movement problem. Player rotates to right. 1 Answer

Fixed tilt rotation on speed up problem 0 Answers

Having trouble moving at right angles properly 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