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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Steve L · Feb 09, 2015 at 05:35 PM · positionplayerreturnoriginreturn value

How can I make my player rotate back after key release?

Hello Unity folks,

In my 2D game I have a floating character which is the main player. If the player is floating towards the left it is leaning left till it reaches 45 degrees on the z axis of Rotation. My question is; How can I make it rotate back to start position when key is released?

This is the script to make it lean to 45 degrees in both direction;

 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);
 
                 }
     }
 }

I have tried this code to make it return to start position but it looks strange as the player is not returning slowly to its position:

 if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D)) {
              gameObject.transform.rotation = Quaternion.identity;
          }
 
 
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
0
Best Answer

Answer by zharik86 · Feb 09, 2015 at 07:16 PM

If you want create smooth rotate back, use SLerp and see simple example below (write on CSharp):

  private bool isBack = false;

  void Update() {
   //This your code with Getkey
   if(Input.GetKey(KeyCode.D)) {
    isBack = false; //in each condition, contains GetKey add this line
    transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
   }
   //...
   //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, 2.0f * Time.deltaTime);
    //Check, if end rotation
    if (Quaternion.Angle(transform.rotation, Quaternion.identity) < 2.0f) {
     transform.rotation = Quaternion.identity;
     isBack = false;
    }
   }
  }

But, better use always Quaternion instead eulerAngle. I hope that it will help you.

Comment
Add comment · Show 3 · 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 Steve L · Feb 09, 2015 at 07:34 PM 0
Share

Thanks for the quick reply :) I have added it in my script but nothing has changed. Can u see what I have done wrong?

 using UnityEngine;
 using System.Collections;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     public float speed = 1f;
     private Vector3 myRotation;
     private bool isBack = false;
     
     void Start() {
         myRotation = gameObject.transform.rotation.eulerAngles;
     }
     
     void Update() {
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
             transform.position += new Vector3
                 (speed * Time.deltaTime, 0.0f, 0.0f);
         
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
             transform.position -= new Vector3
                 (speed * Time.deltaTime, 0.0f, 0.0f);
         
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
             myRotation.z = $$anonymous$$athf.Clamp(myRotation.z - 1f, -45f, 45f);
             transform.rotation = Quaternion.Euler(myRotation);
         }
         
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
                         myRotation.z = $$anonymous$$athf.Clamp (myRotation.z + 1f, -45f, 45f);
                         transform.rotation = Quaternion.Euler (myRotation);
 
             if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
                 isBack = false; //in each condition, contains Get$$anonymous$$ey add this line
                 transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
 
                 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
                     isBack = false; //in each condition, contains Get$$anonymous$$ey add this line
                     transform.position -= new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
             }
             //...
             //Than check up key and doing smooth rotate
             if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D)) {
                 isBack = true;
             }
             if (isBack) {
                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, 2.0f * Time.deltaTime);
                 //Check, if end rotation
                 if (Quaternion.Angle(transform.rotation, Quaternion.identity) < 2.0f) {
                     transform.rotation = Quaternion.identity;
                     isBack = false;
 
                 }
     }
 }
 
         }
     }
 }
 
avatar image zharik86 · Feb 10, 2015 at 07:14 AM 0
Share

What you meen, say nothing change? Rotate not smooth or not rotate. I think, I write you code, base your example, when your player smooth rotate and when Get$$anonymous$$ey and when Get$$anonymous$$eyUp; without eulerAngles.

  public float speed = 1.0f;
  public float speedRot = 1.0f;
  private bool isBack = false;

  void Update() {
   //$$anonymous$$ove right
   if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
    isBack = false;
    transform.position += new Vector(speed * Time.deltaTime, 0.0f, 0.0f);
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, -45.0f), speedRot * Time.deltaTime);
   }
   if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
    isBack = false;
    transform.position -= new Vector(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.Get$$anonymous$$eyUp($$anonymous$$eyCode.A) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.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;
    }
   }
  }
avatar image Steve L · Feb 10, 2015 at 01:13 PM 0
Share

O$$anonymous$$G zharik U ARE $$anonymous$$Y HERO!!! I was looking so long for this solution :D Thank you so much!

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

Camera rotation around player while following. 6 Answers

unity game 1 Answer

Dose any one know how to send object current postion to preivues posistion 1 Answer

Check if player position is greater than one value and less than another? 1 Answer

How I can save mouse position ? 0 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