0
\$\begingroup\$

I have this code that rotates my object:

      private void FixedUpdate()
        {
            transform.Rotate(0, 0, Speed * Time.deltaTime);
            Debug.Log(Speed);
        }
    

When I call this function, I want the object to start rotating in the opposite direction (this works):

        public void changeDir()
        {
            Speed = -Speed;
        }

When I call these functions, I want to change the speed of rotation, while keeping the direction (clockwise or counterclockwise) the same. But these always make the object rotate to the right instead of left:

        public void ZoomSpeed()
        {
            Speed = 330f;
        }
    
        public void ZoomNormal()
        {
            Speed = 125f;
    }

How do I maintain the same direction (sign) when changing the rotation speed?

\$\endgroup\$
1
  • \$\begingroup\$ Could you show us, from where you are calling changeDir()? \$\endgroup\$ Mar 15 at 12:13

1 Answer 1

0
\$\begingroup\$

I'm guessing you might be looking for something like this?

public void ZoomSpeed()
{
    Speed = 330f * Mathf.Sign(Speed);
}

public void ZoomNormal()
{
    Speed = 125f * Mathf.Sign(Speed);
}

That is, you want to change the magnitude of the Speed variable, while keeping its sign? (So if Speed was less than zero before calling ZoomSpeed(), it will be -300 afterward, instead of +300?)

\$\endgroup\$
1
  • \$\begingroup\$ THANK YOU THANK YOU!!! \$\endgroup\$ Mar 15 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .