- Home /
Adjust Yaw For A Gliding (Like Superman 64) Script
This is a little bit different of a setup from the question answered elsewhere in the forums.
My character should glide like Superman, rolling and pitching with the joystick, and levelling when the joystick is neutral. (e.g. like this)
In addition, the character should rotate around the global Y-axis.
I am so close, but missing something, and I think I just need a fresh set of eyes. Currently, the pitch and roll behave properly, but I cannot figure out how to add the previous Y-axis rotation to accumulate the rotation:
public void AdjustGlidePitchYawAndRoll(float pitch, float yaw, float roll, float speed)
{
Quaternion desiredRotation = Quaternion.Euler(new Vector3(0.0f, yaw, 0.0f)) * //I can't figure out how to add the previous yaw properly
Quaternion.Euler(new Vector3(90.0f + pitch, 0.0f, 0.0f)) * //90 degrees, to make the character prostrate by default, like Superman
Quaternion.Euler(new Vector3(0.0f, roll, 0.0f));
Quaternion rotationThisFrame = Quaternion.Slerp(transform.rotation, desiredRotation, speed * Time.deltaTime);
transform.rotation = rotationThisFrame;
}
This code will only rotate toward the current yaw passed in. How would I get the current yaw so I can increase it each frame? Simply using transform.eulerAngles.y does not work, because it can change depending on the pitch and roll.
I think what I ultimately want to do is use the current roll as a starting point, but I can only figure out how to start from the identity. :(
Here's a package that has my code thus far (GlideRotation1.cs is current code). Maybe someone can open it and see the simple fix for it... :-\
Answer by prof · Nov 11, 2014 at 08:57 AM
http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html transform.eulerAngles.y + yaw
in your case
Thanks for contributing, prof! I tried using eulerAngles.y, but it doesn't quite cut it. I believe it would work if the character weren't pitching and rolling.
I also tried doing Translate.Rotate() for each axis, pitch and roll in Space.Self, and yaw in Space.World. I don't remember why that one didn't work. I'm at work at the moment, so I can't run the code now, but in about 9 hours, once I'm home, I'll try your solution and the Transform.Rotate(), and update this post.
This should give you idea (hopefully)
float yaw = 0.0f;
float pitch = 0.0f;
float roll = 0.0f;
float $$anonymous$$ax$$anonymous$$ch = 90.0f;
float $$anonymous$$in$$anonymous$$ch = -30.0f;
float $$anonymous$$axRoll = 30.0f;
float RotSpeed = 25.0f;
void Update () {
pitch += Input.GetAxis("$$anonymous$$ch") * Time.deltaTime * RotSpeed;
yaw += Input.GetAxis("Yaw") * Time.deltaTime * RotSpeed;
roll += Input.GetAxis("Roll") * Time.deltaTime * RotSpeed;
pitch = $$anonymous$$athf.Clamp(pitch, $$anonymous$$in$$anonymous$$ch, $$anonymous$$ax$$anonymous$$ch);
roll = $$anonymous$$athf.Clamp (roll, -$$anonymous$$axRoll, $$anonymous$$axRoll);
transform.rotation = Quaternion.Euler (pitch, yaw, roll);
}
code for learning purposes and unusable. Played Superman 64? Pretty much it)
I want it to work exactly like superman 64! (Like this.)
I see where you're co$$anonymous$$g from, but it's not quite it. Here's a package with the method you coded and the method I coded (there are two scripts attached to the cube you can enable/disable).
This line: transform.rotation = Quaternion.Euler (pitch, yaw, roll);
won't work for my project, because of the order of rotation (yaw and roll effectively do the same thing; you can see this in the GlideRotation2 script).
In the same way, you can try adjusting the yaw in GlideRotation1.cs by using the current transform.eulerAngles.y, and see how weird the behavior gets! :)
So far, I am at a complete loss as to how to get this to work.
Your answer
Follow this Question
Related Questions
pitch yaw roll user input on an object? 2 Answers
pitch camera up and down... 1 Answer
Yaw, Pitch, Roll adjustments relative to object? 1 Answer
How to change angle-roll object with mobile input? 0 Answers