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 Steve L · Jan 03, 2015 at 11:01 AM · rotationpositionstartz axis

How to make rotation return to starts after key release? #c

Hello everybody,

I am struggling with a problem. my game character which is floating is able to lean 45 degrees to the left when going left and vice versa. my question is how can i return the rotation on the z axis to start position(0 degrees) in a slow movement? i have been working with a code which is not solving the problem. it is not giving any faults but strange things occurs.

the code i have now to make it go leaning to 45 degrees is:

 using UnityEngine;
  using System.Collections;
  
  public class PlayerMovement : MonoBehaviour {
      public float speed = 1f;
      private Vector3 myRotation;
      
      void Start() {
          myRotation = gameObject.transform.rotation.eulerAngles;
      }
      
      void Update() {
          if(Input.GetKey(KeyCode.D))
              transform.position += new Vector3
                  (speed * Time.deltaTime, 0.0f, 0.0f);
          
          if(Input.GetKey(KeyCode.A))
              transform.position -= new Vector3
                  (speed * Time.deltaTime, 0.0f, 0.0f);
          
          if(Input.GetKey(KeyCode.D)) {
              myRotation.z = Mathf.Clamp(myRotation.z - 1f, -45f, 45f);
              transform.rotation = Quaternion.Euler(myRotation);
          }
          
          if(Input.GetKey(KeyCode.A)) {
              myRotation.z = Mathf.Clamp(myRotation.z + 1f, -45f, 45f);
              transform.rotation = Quaternion.Euler(myRotation);
          }
      }
  }

the code i was working with is:

 if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A)) {
                 gameObject.transform.rotation = Quaternion.identity;    }

what should i apply to my first script or change to the script above?

any recommendation would be appreciated.

thanks in advance!

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
1
Best Answer

Answer by Steve L · Feb 10, 2015 at 01:22 PM

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     public float speed = 5.0f;
     public float speedRot = 5.0f;
     private bool isBack = false;
     
     void Update() {
                 //Move right
                 if (Input.GetKey (KeyCode.D)) {
                         isBack = false;
                         transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
                         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, -45.0f), speedRot * Time.deltaTime);
                 }
                 if (Input.GetKey (KeyCode.A)) {
                         isBack = false;
                         transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
                         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, 45.0f), speedRot * Time.deltaTime);
                 }
                 //Than check up key and doing smooth rotate
                 if (Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.D)) {
                         isBack = true;
                 }
                 if (isBack) {
                         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.identity, speedRot * Time.deltaTime);
                         //Check, if end rotation
                         if (Quaternion.Angle (transform.rotation, Quaternion.identity) < 2.0f) {
                                 transform.rotation = Quaternion.identity;
                                 isBack = false;
                         }
                 }
         }    }

Found it myself ;)

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make Camera position Independent of its Rotation? 1 Answer

Unfreezing rotation causes the position and rotation go crazy 0 Answers

Problem with rotation of objects 0 Answers

Firing towards mouse- aim in all directions? 1 Answer

navmesh agent updatePosition updateRotation: what's this? 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