2

I am trying to add a parachute in my game, and I need it to rotate 45 degrees right and then 45 degrees left and keep alternating, diagram of how i need to work below

enter image description here

The code that i am using is as follows, however it only works on 1 side then doesn't go back.

        if (maxRotation <= transform.rotation.eulerAngles.z)
        {
            rotatingRight = false;
        }else if(minRotation <= transform.rotation.eulerAngles.z)
        {
            rotatingRight = true;
        }

        transform.Translate(Vector2.down * fallSpeed * Time.deltaTime);

        if (rotatingRight)
        {
            transform.Rotate(Vector3.forward * 45 * Time.deltaTime);
        }
        else
        {
            transform.Rotate(Vector3.forward * -45 * Time.deltaTime);
        }
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    Change to `else if(minRotation >= transform.rotation.eulerAngles.z)`. Note the `>=` vs your `<=`. – 3Dave Feb 04 '20 at 18:55

3 Answers3

1

As 3Dave said in the comments, it's mostly that you should rotating right when minRotation is greater than or equal to eulerAngles.

if (maxRotation <= transform.rotation.eulerAngles.z)
{
    rotatingRight = false;
} else if (transform.rotation.eulerAngles.z <= minRotation)
{
    rotatingRight = true;
}

transform.Translate(Vector2.down * fallSpeed * Time.deltaTime);

if (rotatingRight)
{
    transform.Rotate(Vector3.forward * 45 * Time.deltaTime);
}
else
{
    transform.Rotate(Vector3.forward * -45 * Time.deltaTime);
}

I realize now that the previous version of this answer was needlessly convoluted but I can't delete it now that it's accepted. The problem was simply caused by a typo.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
-1

Your code can be simplified quite a lot. You actually wouldn't need a boolean to see if you're going right. Just do it in the first if, no need for a second one. Also there's a good chance you are going to reduce the amount of error that can slip in like here where your boolean isn't always what you think it is.

float rotationValue = 45;

if(transform.rotation.eulerAngles.z >= maxRotation)
{
    rotationValue = -45;
}
else if(transform.rotation.eulerAngles.z <= minRotation)
{
    rotationValue = -45;
}

transform.Translate(Vector2.down * fallSpeed * Time.deltaTime);


transform.Rotate(Vector3.forward, rotationValue * Time.deltaTime);
-1

I was able to solve it by doing this

        if (maxRotation == Math.Round(transform.rotation.eulerAngles.z))
        {
            rotatingRight = false;
        }else if(minRotation == Math.Round(transform.rotation.eulerAngles.z))
        {
            rotatingRight = true;
        }

        transform.Translate(Vector2.down * fallSpeed * Time.deltaTime);

        if (rotatingRight)
        {
            transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
        }
        else
        {
            transform.Rotate(Vector3.back * rotationSpeed * Time.deltaTime);
        }
  • 1
    please don't do this - if a lag spike occurs at a bad time, causing `Time.deltaTime` to be huge, you might rotate the rotation beyond `minRotation` and `maxRotation`. you don't guarantee that it won't go above `maxRotation` or below `minRotation`, so it would then continue to rotate in one direction forever. – Ruzihm Feb 04 '20 at 20:30
  • Ah I see, do you by any chance have a solution on how i can achieve this in a proper way? – Matthew Scicluna Feb 04 '20 at 20:35
  • My answer is fine, but one of the best methods would be to use something like Mathf.PingPong to get the z eulerAngle you need that frame – Ruzihm Feb 04 '20 at 21:04