I’m trying to make a script that rotates a moving ship up and down when one of two buttons are pressed.
The code I have done works but there is a noticeable stutter when rotating, like it would rotate, move, rotate, move, rotate and move instead of a nice fluid turn.
The code I am using is
void FixedUpdate()
{
if (getHealth.isAlive == true)
{
GetComponent<Rigidbody2D>().velocity = transform.right * forwardVelocityValue;//object to move.velocity = speed (x,y)
}
if (getHealth.isAlive == false)
{
GetComponent<Rigidbody2D>().velocity = transform.up * 2;//object to move.velocity = speed (x,y)
}
if (isRotatingUp == true)
{
if (transform.localEulerAngles.z < maximumUpRotation) //if rotation is 80 or less then do
{
heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
}
if (transform.localEulerAngles.z > minimumUpRotation) //if rotation is 255 or more then do
{
heroObject.transform.Rotate(0, 0, spinSpeed);//Object to spin.Startspinning.angle.z
}
isRotatingUp = false;
}
if (isRotatingDown == true)
{
if (getHealth.isAlive == true)
{
if (transform.localEulerAngles.z < minimumDownRotation) //if rotation is 85 or less then do
{
heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z
}
if (transform.localEulerAngles.z > maximumDownRotation) //if rotation is 250 or more then do
{
heroObject.transform.Rotate(0, 0, -spinSpeed);//Object to spin.Startspinning.angle.-z
}
}
isRotatingDown = false;
}
so what I am trying to do is make a nice fluid constantly moving ship (no control over the forward but gets faster the further you go) that when dies stops and just slowly moves up.
1st problem is you need to cache the rigidbody so you don’t keep calling the expensive GetComponent method every fixedUpdate. Move the GetComponent to Start/Awake/OnEnable so it only does it once and keeps a reference around
public class MyClass : MonoBehaviour
{
RigidBody2D myRigidBody;
void Awake()
{
myRigidBody = GetComponent<RigidBody2D>();
}
}
2nd problem is your if statements need some adjusting so the logic suits what you want to do.
void FixedUpdate()
{
if (getHealth.isAlive)//No need for == true
{
myRigidBody.velocity = transform.right * forwardVelocityValue;
if (isRotatingUp)
{
heroObject.transform.Rotate(0, 0, spinSpeed);
}
else if (isRotatingDown)
{
heroObject.transform.Rotate(0, 0, -spinSpeed);
}
}
else //Same as == false
{
myRigidBody.velocity = transform.up * 2;
}
}
Try this and see if it helps. I see other potential issues, but I need to see how you are setting isRotatingUp and isRotatingDown.
OnGUI() isn’t really supposed to be used for game play anymore. Try using something else like the new UI buttons or even Input.GetKey() in update to test it in the editor.
rotating via the transform might not work as expected when you are using rigidbody. Try using the rigidbody to rotate it.
They are using FixedUpdate so not really needed to have Time.deltaTime or even Time.fixedDeltaTime.
If it was Update then that would have been my first suggestion as it is frame dependent.