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 jthomas117 · Jan 29, 2014 at 11:42 AM · c#rotationmovementquaternionbeginner

Quaternion and rotation of object

I am currently remaking a game that was a 2D college project, it is a 1945 clone. I have the movement controls sorted, now I am trying to rotate the player object left and right around 30 degrees, using a Quaternion.Lerp, so that it adds more of a feel to the horizontal movement.

I know a little C#, but apparently not enough as right now I am lost, I would really appreciate some guidance and some assistance. Thank you in advance!

here is the movement script, currently the only script in the game.

 using UnityEngine;
 using System.Collections;
 
 public class MovementScript : MonoBehaviour {
 
     public float speed = 300f;
     public float xVelocity;
     public float borderX = 15;
     public float borderY = 10;
 
     void Update()
     {
         Movement();
     }
 
     void Movement() 
     {
         Vector3 pos = transform.position;
         Quaternion rot = transform.rotation;
 
         float moveHoriz = Input.GetAxis("Horizontal");
         float moveVert = Input.GetAxis("Vertical");
 
         xVelocity = rigidbody.velocity.x;
 
         float horizCalc = moveHoriz * speed * Time.deltaTime;
         float vertCalc = moveVert * speed * Time.deltaTime;
 
         float rotationAmount = 30;
 
         rigidbody.AddForce(horizCalc, 0, vertCalc);
         pos.x =  Mathf.Clamp(transform.position.x, -borderX, borderX);
         pos.z =  Mathf.Clamp(transform.position.z, -borderY, 0);
         transform.position = pos;
 
         transform.rotation = Quaternion.Lerp(Quaternion.Euler(Vector3(rot.x, rot.y, rot.z)), Quaternion.Euler(new Vector3(rot.x, rot.y,rot.z(rotationAmount * moveHoriz))), 1 * Time.deltaTime);
 
         if(pos.x <-borderX + 0.1 || pos.x > borderX - 0.1)
         {
             //xVelocity = 0f;
             if(moveHoriz <-0.1f)
                 xVelocity = -1f;
             if(moveHoriz >0.1f)
                 xVelocity = 1f;
         } 
     }
 }
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 njpatter · Jan 29, 2014 at 01:51 PM 0
Share

If you are just having problems with the rotation, here are a couple things...

Try Quaternion.Slerp ins$$anonymous$$d.

Also, the 'time' variable that you pass Slerp or Lerp at the end goes from 0 to 1, so by setting it equal to 1 * Time.deltaTime you will never rotate past a small fraction of the desired rotation.

1 Reply

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

Answer by gfoot · Jan 29, 2014 at 02:21 PM

I'll explain the most obvious problem with your current code, then list a few common ways you can get the rotation effect. I think you should try them all, and hopefully one will work for you or at least you'll get a better understanding of things.

From your code:

 Quaternion rot = transform.rotation;
 ...
 Quaternion.Euler(Vector3(rot.x, rot.y, rot.z))

This is not right - the x, y, z properties of quaternions are not angles. What you want here is probably just "rot", which is already a quaternion and can be passed straight in without decomposing to Euler angles and back.

For the other argument you wrote:

 Quaternion.Euler(new Vector3(rot.x, rot.y,rot.z(rotationAmount * moveHoriz)))

maybe you wanted something like this:

 Vector3 rotEuler = rot.eulerAngles;
 Quaternion.Euler(new Vector3(rotEuler.x, rotEuler.y, rotEuler.z + rotationAmount * moveHoriz))

But, overall it is much simpler to just do something like this, instead of the lerp:

 transform.rotation *= Quaternion.Euler(0, 0, rotationAmount * moveHoriz * Time.deltaTime);

This would normally make your ship rotate at a rate dependent on the horizontal input, without any inertia. You are using a rigidbody, though, so it's not going to work well as the rigidbody will also be trying to drive transform.rotation. You might get better results in that case from:

 rigidbody.angularVelocity = new Vector3(0, 0, rotationAmount * moveHoriz);

This sets the rigidbody's angular velocity to whatever rotation speed you want, and lets the rigid body itself apply that back to the transfrom. It is still very crude.

The other thing to consider is whether you want to embrace the angular inertia that rigidbody is trying to provide for you, instead of stomping on it. So instead of directly modifying rotation or angularVelocity, you can apply torques to the rigidbody:

 rigidbody.AddTorque(new Vector3(0, 0, rotationAmount * moveHoriz));

Adjust 'rotationAmount' and the rigidbody's angular damping in the Inspector to tune the ramp-up rate, the maximum achieved rotation rate, and the ramp-down rate when you release the controls.

[1]: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-inertiaTensor.html

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 jthomas117 · Jan 29, 2014 at 04:47 PM 0
Share

Thank you so much for your help, I managed to get around the problem by using a different method but still gaining the desired result, although I learned a lot from your detailed answer that really helped! I will note all of this down for future reference, thanks again!

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

Rotation reversing for a single frame while turning. 0 Answers

Player model rotates forward with camera 0 Answers

Simple Rotation 1 Answer

Ball Rolling Animation Not Working Properly 0 Answers

Rotating an object towards target on a single axis 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