Auto level an object
Hi I'm making a fish game and I'm trying to have the fish be able to self right its self over the z axis. Currently my problem is it will lock the z rotation at 0 and it can't be altered without returning to 0 instantly. My goal is to have the fish be able to self right over the z axis over time after being altered. It must also keep its rotation over the x and y axis.
Blockquote
private void Rotate()
{
//calculate rotation
float rotateAroundY = yawLookInput * rotationSpeed * Time.deltaTime;
float rotateAroundX = -pitchLookInput * rotationSpeed * Time.deltaTime;
float rotateAroundZ = Mathf.LerpAngle(transform.rotation.z, 0, .5f);
//rotate
Vector3 rot = transform.eulerAngles;
rot = new Vector3(rotateAroundX + transform.eulerAngles.x, rotateAroundY + transform.eulerAngles.y, rotateAroundZ);
transform.eulerAngles = rot;
//transform.Rotate(rotateAroundX, rotateAroundY, rotateAroundZ);
}
Could we get a sample of the script you are using?
I plugged that into this behavior, which let me test it using the inspector.
using UnityEngine;
public class RotateIt : $$anonymous$$onoBehaviour
{
public float yawLookInput;
public float rotationSpeed;
public float pitchLookInput;
private void Update()
{
Rotate();
}
private void Rotate()
{
//calculate rotation
float rotateAroundY = yawLookInput * rotationSpeed * Time.deltaTime;
float rotateAroundX = -pitchLookInput * rotationSpeed * Time.deltaTime;
float rotateAroundZ = $$anonymous$$athf.LerpAngle(transform.rotation.z, 0, .5f);
//rotate
Vector3 rot = transform.eulerAngles;
//transform.rotation = new Quaternion(rotateAroundX + transform.eulerAngles.x, rotateAroundY + transform.eulerAngles.y, rotateAroundZ, 0);
transform.Rotate(rotateAroundX, rotateAroundY, rotateAroundZ);
}
}
Both with your original version and with my slight edit, I didn't have the problem of the Z rotation being locked. What did happen was that the Z axis didn't change if I only had the pitch control set to something but not the yaw. Likewise, only setting the yaw caused it to spin about the Y axis unless there was already some existing rotation.
In the commented out section, I tried just setting the rotation to the calculated values. In this case, still, the rotation would occur but the axes affected were transposed.
I think we're going to need a better statement of what you want want and what you're not getting. $$anonymous$$aybe try the Given-When-Then structure for stating what you want - that is: 1. Explain in exact detail an example starting state of the system (given). 2. Explain in exact detail the thing that causes Rotate() to be invoked (when). 3. Explain in exact detail what the final state of the system you want is after Rotate() was invoked (then).
If there are any details in the "then" part that cannot be traced back to the "given" and/or "when" part, then the explanation of what you want isn't precise enough. It's got to be so precise that you could execute it as a test to help you and help us help you.
Once you've done that, you can explain what's actually happening in contrast with your expectation.
As a side note, I would point out that the docs for eulerAngles says it's meant to be read and won't work if you push anything past 360.
Without more details and based on what little information I can glean, I would say you might consider a different strategy: pick a target rotation, and create forces that push the fish back toward your ideal rotation and make the forces stronger the further from your target level they are. Your input system can then push rotation however it likes and your balance system can just push back as hard as it needs to for the game to work the way you want.
Your answer
Follow this Question
Related Questions
GameObject not looking at me.. 1 Answer
Flip 3D Character Rotation 180 on Y axis 1 Answer
How to rotate a camera slowly along a tween? 1 Answer
How can I set rotation properly? 0 Answers
Weird rotation when using Slerp!!? 3 Answers