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 /
  • Help Room /
avatar image
0
Question by Alsador · Oct 29, 2015 at 04:01 PM · rotationmovementquaternionx-axis

Question about rotating an object thats moving.

So i have a basic 2D movement script, i would like to have it so it slight rotates right when moving right and slightly rotate when moving left. But once the buttons used to move are released I would want it to return to it's original angle. I just do have any idea on how to go about this. Any insight would be great.

Comment
Add comment · Show 2
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 whogas · Oct 31, 2015 at 03:37 AM 0
Share

The best place to start would be reading about Quaternion.Lerp

You can add a this to your object so that when you move in a direction, it also rotates with quaternion.lerp.

avatar image Alsador whogas · Nov 01, 2015 at 08:11 PM 0
Share

Im still relatively new to C# and coding. I dont understand Quaternion too well. Ive been trying to figure it out about i cant use int's for the the A and B part of Quaternion.Lerp. I'm sure if i can get the base tilt with the movement i can figure out how to return it to its original rotation. I'm truly stumped.

1 Reply

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

Answer by Statement · Nov 01, 2015 at 08:48 PM

You could set a target angle with your input, and then use Mathf.MoveTowardsAngle or similar functions.

Something like this (psuedo code)

 if (left)
    target = -45;
 if (right)
    target = 45;
 
 angle = Mathf.MoveTowardsAngle(angle, target, speed * Time.deltaTime);

Here's an example using Mathf.SmoothDampAngle to give a smooth Z axis rotation. You can setup a bunch of different parameters and press A/D keys (it uses horizontal axis by default). You can also override the tilt with a script of your choice. Just set .tilt to a value between -1 and 1. 0 is forward. The script will overwrite rotation so if you have any other script that is setting rotation you have to modify the code or make either of the scripts a child of the other.

 using UnityEngine;
 
 public class Rotate2D : MonoBehaviour
 {
     // Max tilt angle
     [Range(0, 360)]
     public float tiltAngle = 45;
 
     // Degrees per second
     [Range(0, 720)]
     public float speed = 360f;
 
     // Smaller values is snappier
     [Range(0.01f, 1)]
     public float smoothTime = 0.1f;
 
     // For the lazy, some kind of def. impl.
     public bool useHorizontalAxis = true;
 
     // Provided by script if not useHorizontalAxis
     [Range(-1, 1)]
     public float tilt;
 
     // The current rotation in angles around Z axis
     [HideInInspector]
     public float angle;
 
     // The current angular velocity
     [HideInInspector]
     public float velocity;
 
     void Update()
     {
         DefaultTiltBehaviour();
         UpdateLocalRotation();
     }
 
     void DefaultTiltBehaviour()
     {
         if (useHorizontalAxis)
             tilt = -Input.GetAxisRaw("Horizontal");
     }
 
     void UpdateLocalRotation()
     {
         float target = tiltAngle * Mathf.Clamp(tilt, -1, 1);
         angle = Mathf.SmoothDampAngle(angle, target, 
                                   ref velocity, smoothTime, speed, Time.deltaTime);
         transform.localRotation = Quaternion.Euler(0, 0, angle);
     }
 }
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 Alsador · Nov 01, 2015 at 10:19 PM 0
Share

Thank you so much this work perfectly!!

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

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

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer

try to rotate a character controller when translating on z only 0 Answers

Rotate to a given angle with Rigidbody2D 1 Answer

Resident Evil 4 Style Aiming Window - Rotational Problem When Aiming 1 Answer

Know when the object is rotated? 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