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 /
avatar image
0
Question by laurienash · Jun 19, 2016 at 01:43 PM · c#rotationquaternionbasic programmingquaternion.slerp

Transform rotation and position on key input: rotation only working the first time

Hi,

I want to rotate and move a game object to a set position and rotation. The transform.position is working fine, but the rotation only works the first time I press the q and w key, but after that it doesn't rotate the object to the correct position.

I just can't work out what I'm doing wrong - I thought maybe I wasn't resetting a value correctly, but I can't see what I'm missing.

So I want the object to rotate to 9 degrees on the x axis every time q is pressed, and 45 degrees on the x axis when w is pressed.

Does anyone have any ideas on what I'm doing wrong?

Best, Laurien

Edit: updated script

  using UnityEngine;
 using System.Collections;
 
 public class MovementRotationCoroutine : MonoBehaviour {
 
     public GameObject cameraMain;
 
     private float degree;
     private float angle;
 
 
 
 
     void Update () {
 
         if (Input.GetKeyDown("q"))
         {
             degree = 9f;
             StartCoroutine ( MoveToPositionPersp (new Vector3(0,0.5f,-5.5f), 2f, 2f));
         }
 
 
         if (Input.GetKeyDown("w"))
         {
             degree = 45f;
             StartCoroutine ( MoveToPositionOrtho (new Vector3(0,5.5f,0), 2f, 2f));
         }
     }
 
 
 
 
     public IEnumerator MoveToPositionOrtho(Vector3 position, float timeToMove, float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
 
 
         var currentPos = transform.position;
         var currentPosRotate = transform.rotation;
         var t = 0f;
     
 
         while(t < 1)
         {
             t += Time.deltaTime / timeToMove;
 
             transform.position = Vector3.Lerp(currentPos, position, t);
 
             angle = Mathf.LerpAngle(transform.rotation.x, degree, t);
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);
 
             yield return null;
         }
     }
 
 
 
 
 
         public IEnumerator MoveToPositionPersp(Vector3 position, float timeToMove, float waitTime)
         {
             yield return new WaitForSeconds(waitTime);
 
             var currentPos = transform.position;
             var currentPosRotate = transform.rotation;
             var t = 0f;
             
             
             while(t < 1)
             {
                 t += Time.deltaTime / timeToMove;
                 transform.position = Vector3.Lerp(currentPos, position, t);
                 
                 angle = Mathf.LerpAngle(transform.rotation.x, degree, t);
                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);
                 
                 yield return null;
             }
     }
     
 }

Comment
Add comment · Show 6
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 ninja_gear · Jun 19, 2016 at 02:57 PM 0
Share

im not sure but maybe

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), Time.deltaTime);

should be

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);
avatar image laurienash · Jun 19, 2016 at 04:15 PM 0
Share

Thanks - you're right, I think Time.deltaTime should have been t, the speed of rotation works as expected now.

But - it's still not rotating correctly, it seems to be doubling the value I want it to rotate to each time.

If the camera starts at 9 degrees, and I press 'w' first it rotates to 45 degrees correctly, if I press 'q' after this it rotates back to 9 degrees correctly.

But after that if I alternate between pressing 'w' and 'q' the values go:

45 9 54 18 63 27 etc.

I've updated the script with t ins$$anonymous$$d of time.deltaTime, and also changed this line:

 if (Input.Get$$anonymous$$eyDown("q"))
         {
             degree += 36f;
             StartCoroutine ( $$anonymous$$oveToPositionPersp (new Vector3(0,0.5f,-5.5f), 2f, 2f));
         }

to this:

 if (Input.Get$$anonymous$$eyDown("w"))
     {
         degree += 45f;
         StartCoroutine ( $$anonymous$$oveToPositionOrtho (new Vector3(0,5.5f,0), 2f, 2f));
     }
 }

But I'm not sure how to change it so it rotates to a set value each time a key is pressed. (So it always rotates to 9 degrees when 'q' is pressed, and always rotates to 45 degrees when 'w' is pressed).

avatar image JedBeryll laurienash · Jun 19, 2016 at 04:51 PM 0
Share

You mean like if you press w it always rotates to the nearest multiple of 45 and on q it rotates to the multiple of 9?

avatar image laurienash · Jun 19, 2016 at 05:10 PM 0
Share

Oh sorry - no I didn't say that very clearly. I want the rotation of the object to switch between 45 degrees and 9 degrees. (Like a clamped $$anonymous$$ and max rotation). So when w is pressed: the object rotates to 45 degrees. (so in the hierarchy the rotation of the object would always read 45, 0, 0.) When q is pressed the object rotates back to 9 degrees. (so in the hierarchy the rotation of the object would always read 9, 0, 0.)

(at the moment it seems to be rotating in multiples but this isn't what I want)

avatar image ninja_gear laurienash · Jun 19, 2016 at 05:16 PM 0
Share

change the additive nature of:

          if (Input.Get$$anonymous$$eyDown("q"))
          {
              degree -= 36f;
              StartCoroutine ( $$anonymous$$oveToPositionPersp (new Vector3(0,0.5f,-5.5f), 2f, 2f));
          }
  
  
          if (Input.Get$$anonymous$$eyDown("w"))
          {
              degree += 45f;
              StartCoroutine ( $$anonymous$$oveToPositionOrtho (new Vector3(0,5.5f,0), 2f, 2f));
          }

to an absolute:

          if (Input.Get$$anonymous$$eyDown("q"))
          {
              degree = 9f;
              StartCoroutine ( $$anonymous$$oveToPositionPersp (new Vector3(0,0.5f,-5.5f), 2f, 2f));
          }
  
  
          if (Input.Get$$anonymous$$eyDown("w"))
          {
              degree = 45f;
              StartCoroutine ( $$anonymous$$oveToPositionOrtho (new Vector3(0,5.5f,0), 2f, 2f));
          }
avatar image laurienash ninja_gear · Jun 19, 2016 at 06:09 PM 0
Share

Ohh that's really great thank you!! I misunderstood how that worked, just looked that up now!

I've just updated the script now! (If you convert your comment to an answer I can tick it as answered).

So it now rotates correctly - but I was wondering if you knew why this happened?

If I press the 'w' key while the game object is still moving towards the correct rotation and position for 'q', then the rotation switches immediately to the 'w' position, but the translation finishes moving to 'q' and before moving to 'w'.

Ideally I'd want it to pause at whatever position it was and then start rotating and moving back to 'w' (and vice versa).

Do you know how I could go about changing this?

2 Replies

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

Answer by ninja_gear · Jun 19, 2016 at 06:35 PM

Save your coroutines in an ienumerator. When calling one or the other, stop the coroutines, then start up the new one:

IEnumerator _rotating;

If (_rotating != null) StopCoroutine(_rotating); StartCoroutine(_rotating = MoveToPositionPers('args'));

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 laurienash · Jun 19, 2016 at 09:08 PM 0
Share

That's great - it's working perfectly now. Thanks so much!!

avatar image
0

Answer by jarjarb · Feb 21, 2021 at 01:18 PM

may you help me with my code?

what I want to do is make it make my rotations get stored in a quaternion and then change the quaternion to look up and down when the left and right arrow keys are pressed. However I am fairly new to quaternions and unity in general so I have no Idea how to do this.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class look : MonoBehaviour {

 [SerializeField]
 KeyCode up;
 [SerializeField]
 KeyCode down;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKey(up))
     {
         transform.Rotate(-2f, 0f, 0f);
         
     }
     if (Input.GetKey(down))
     {
         transform.Rotate(2f, 0f, 0f);
         
     }
 }

}

please help me with integrating the quaternions into my code.

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 jarjarb · Feb 21, 2021 at 01:21 PM 0
Share

all I would like it to do is clamp my rotations to between -90 and 90 so I am not making it so that when you look down too far you won't look in 180 degrees.

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

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

Flip over an object (smooth transition) 3 Answers

Clock script with custom time not working 0 Answers

few question about quaternion slerp 3 Answers

Trouble with Camera Rotation Math 2 Answers

Weird shake when I try to rotate player according to Virtual Cameras rotation. 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