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 Jammer3000 · Mar 04, 2014 at 04:06 PM · rotationjavascriptrandom

Z rotation locks to certain value randomly?

The code below was working great but I started messing around with Quaternion.Slerp just to test if the rotation would work better with it as everyone been saying, but now when I changed my code back to how it was when it was working it sets the Z rotation to 270 at the start?! I tried forcing it by setting the game objects z rotation to 0 in a function Start(); function but that didn't work either?

This is the code that was working fine:

 #pragma strict
 
 var rotationDownDelay = 1.0f;
 var jumpSound : AudioSource;
 var lookDownTrigger = false;
 
 function Update () {
    if (!lookDownTrigger && Input.GetButtonUp("Fire1")) {
        rigidbody2D.velocity = Vector2(0.0f, 7.0f);
        jumpSound.Play();
        transform.eulerAngles = Vector3(0.0, 0.0, 20.0);
    }
 }
 
 
 function LateUpdate () {
 
 if (rigidbody2D.velocity.y <= 0) {
     lookDownTrigger = true;
     Debug.Log("lookDown");
 //    yield WaitForSeconds (rotationDownDelay);
     Wait();
     transform.eulerAngles = Vector3(0.0, 0.0, -90.0);
     lookDownTrigger = false;
     }
 }
 
 
 function Wait () {
 
     Debug.Log("before wait");
      yield WaitForSeconds (rotationDownDelay);
     Debug.Log("after wait");
 }
Comment
Add comment · Show 9
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 robertbu · Mar 04, 2014 at 04:12 PM 0
Share

You need to explore how coroutines work. When you call 'Wait' the code executes down to the 'yield' and immediately returns. That means it is not waiting inside of LateUpdate(). In addition, since this code will be executed any time 'rigidbody2D.velocity.y

avatar image Jammer3000 · Mar 04, 2014 at 04:14 PM 0
Share

Are you saying that my Wait function is giving it that 270 z rotation at the beginning?

avatar image robertbu · Mar 04, 2014 at 04:26 PM 1
Share

First off, a rotation of 270 is the same as a rotation of -90. Second, I'm assu$$anonymous$$g your starting 'y' velocity is

avatar image Jammer3000 · Mar 04, 2014 at 04:37 PM 0
Share

Oh I see is there a way to keep it at a certain z rotation until the first tap has happened?

avatar image robertbu · Mar 04, 2014 at 05:22 PM 1
Share

A frustration in answering your questions is that we never get a clear idea of what you are really trying to accomplish with your code. So the specific question you ask gets answered (as a comment since folks are not sure), but that is not the 'real' or complete answer. In future question I suggest you spell out exactly what you are trying to do as well as the specific behavior that has you stymied.

Here is a bit of code that rotates over time. It is not intended to be a solution to your problem above, but a demonstration of one way an object can rotate over time that you an learn from. It uses a timer. The function Slerp()s last parameter takes a value between 0.0 and 1.0. When you pass it 0.0, it returns the first value as the rotation. When you pass 1.0, it returns the second value. For any value between 0.0 and 1.0, it gives a rotation that is between the two rotations.

Start a new scene. Add a block to the scene and attach this script. While running, click the left mouse button. It will rotate once smoothly over 1.0 seconds.

 #pragma strict
 
 var rotating = false;
 
 function Update() {
     if (!rotating && Input.Get$$anonymous$$ouseButtonDown(0)) {
         RotateTo(Vector3(0,0,-90), 1.0);
     }
 }
 
 function RotateTo(to : Vector3, time : float) {
     rotating = true;
     var startRot = transform.rotation;
     var endRot = Quaternion.Euler(to);
     
     var timer = 0.0;
     while (timer < time) {
         transform.rotation = Quaternion.Slerp(startRot, endRot, timer/time);
         yield;
         timer += Time.deltaTime;
     }
     transform.rotation = endRot; 
     rotating = false;
 }

YOu might also exa$$anonymous$$e the $$anonymous$$oveObject script in the Unity Wiki. The Rotation() function does a realtive rotation vs. the absolute rotation of this code.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

[SOLVED]Possible Alternation of Transform.Rotate 1 Answer

Spawn gameObjects at random locations in limited area 4 Answers

How to instantiate 2D object at fixed location? 1 Answer

How to rotate object on mouse click? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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