Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by alexanderameye · May 15, 2015 at 03:51 PM · rotationbooleandoorslerpopening

Open and close doors by pressing 'E'

Hey,

I have this structure of code inside an update function:

 if (Input.GetKey(KeyCode.E)){
             Pressed = true;
         }
 
         //rotate
         if (Pressed == true) 
         {
             Slerpfunction 
             //print("End Reached");
             Pressed = false;    
         }
 
         //rotate back
         if (Pressed == true) 
         {
             Slerpfunction
             //print("End Reached");
             Pressed = false;
         }
 }


I know that this won't work, but how can I make it so that when I press 'e' it rotates, then when the rotation is completed and I press 'e' again, it rotates back, and then when I press 'e' again, it rotates again and so on. Thx!

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

Answer by $$anonymous$$ · May 15, 2015 at 04:22 PM

Here is your code, I used Quaternion.Lerp() instead of Slerp to make it simpler but you can replace that code with yours.

 private bool Pressed;
     private int State;
 
     private Quaternion EndRot1, EndRot2;
 
     private void Start()
     {
         EndRot1 = Quaternion.Euler(0, 90, 0); // Could be also set from the Inspector
         EndRot2 = Quaternion.identity;
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.E))
             Pressed = true;
 
         if (Pressed) // Rotation
         {
             if (transform.rotation == (State == 0 ? EndRot1 : EndRot2)) // Did the rotation end?
             {
                 State ^= 1; // Inverts "State" from 0 to 1 and reverse
                 Pressed = false;
             } // Slerp Method Here
             else this.transform.rotation = Quaternion.Lerp(transform.rotation, State == 0 ? EndRot1 : EndRot2, Time.deltaTime * 5);
         }
     }
Comment
Add comment · Show 14 · 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 alexanderameye · May 23, 2015 at 02:10 PM 0
Share

hey I'll take a look at this, haven't got time yet though, thanks for answering!

avatar image $$anonymous$$ · May 23, 2015 at 04:19 PM 1
Share

sure let me know if it was working

avatar image alexanderameye · May 23, 2015 at 06:53 PM 0
Share

hey, just a question, what does the ^= operator do?

avatar image alexanderameye · May 23, 2015 at 06:58 PM 0
Share

and the comparison with the question mark means:

if state == 0 --> rotation = Endrot1 ////// if state == 1 --> rotation = Endrot2

avatar image $$anonymous$$ · May 23, 2015 at 07:40 PM 1
Share

If you define INT variable, C# automaticly creates a 'safe code' where it assigns 0 to value types and NULL to reference types automaticly so you dont have to do it but you can if you want.

x ^= 1 operator simply swaps 0 and 1 value, if you have 1, it negates it to 0 and reverse over an over again :)

Show more comments
avatar image
2

Answer by Griffo · May 23, 2015 at 02:21 PM

Another way is Vector3.Lerp .. But it's not the correct way .. ;)

 #pragma strict
 
 var openTime : float;
 var openPosition : Vector3;
 
 private var originalRotation : Vector3;
 private var targetRotation : Vector3;
 private var originalTime : float;
 private var originalOpenTime : float;
 private var z : int;
 
 function Update ()
 {
     if (Input.GetKeyDown ("e"))
     {
         RotateObject();
     }
 }
 
 function RotateObject ()
 {
     z = (z % 2) + 1;
     if(z == 1)
     {
         originalRotation = transform.localRotation.eulerAngles;
         targetRotation = transform.localRotation.eulerAngles + openPosition;
         originalOpenTime = openTime;
 
         while (openTime > 0.0f)
         {
             openTime -= Time.deltaTime;
             transform.localRotation.eulerAngles = Vector3.Lerp(targetRotation, originalRotation, openTime / originalOpenTime);
             yield;
         }
         openTime = originalOpenTime;
 
     }else{
 
         originalRotation = transform.localRotation.eulerAngles;
         targetRotation = transform.localRotation.eulerAngles - openPosition;
         originalOpenTime = openTime;
 
         while (openTime > 0.0f)
         {
             openTime -= Time.deltaTime;
             transform.localRotation.eulerAngles = Vector3.Lerp(targetRotation, originalRotation, openTime / originalOpenTime);
             yield;
         }
         openTime = originalOpenTime;
     }
 }

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 alexanderameye · May 23, 2015 at 08:13 PM 0
Share

hey, sorry but I looked first at the other guys solution and it worked, but thank you so much for your effort of even answering this :D

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

Slerp doesnt go to the rotation its supposed to go to 1 Answer

Best way to rotate a door 1 Answer

How to open and close a door parented to a rotating object? 0 Answers

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers

Basic AI Locked Axis 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