- Home /
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");
}
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
Are you saying that my Wait function is giving it that 270 z rotation at the beginning?
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
Oh I see is there a way to keep it at a certain z rotation until the first tap has happened?
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.