Skip to main content
edited body
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

If your goal is to rotate to the opposite of the current facing direction, that would be -transform.forward, not -transform.position as you're currently using in your code. Positions and directions are different things.

It looks like you want something more like this:

Quaternion targetOrientation;

void Update()
{
    if (exitSpaceShipSurroundingArea)
    {
        if (slowd) {
            SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0) 
            {
                slowd = false;                
                // Cache the 180 degree direction change once, when we start rotating.
                targetRotation = Quaternion.LookRotation(-transform.forward);
            }
        } 
        else
        {
            // Rotate toward our cached orientation.
            transform.rotation = Quaternion.RotateTowards(
                  transform.Rotationrotation,
                  targetOrientation,
                  Time.deltaTime * 180f / secondsToRotate
            );
        }
    }
}

If your goal is to rotate to the opposite of the current facing direction, that would be -transform.forward, not -transform.position as you're currently using in your code. Positions and directions are different things.

It looks like you want something more like this:

Quaternion targetOrientation;

void Update()
{
    if (exitSpaceShipSurroundingArea)
    {
        if (slowd) {
            SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0) 
            {
                slowd = false;                
                // Cache the 180 degree direction change once, when we start rotating.
                targetRotation = Quaternion.LookRotation(-transform.forward);
            }
        } 
        else
        {
            // Rotate toward our cached orientation.
            transform.rotation = Quaternion.RotateTowards(
                  transform.Rotation,
                  targetOrientation,
                  Time.deltaTime * 180f / secondsToRotate
            );
        }
    }
}

If your goal is to rotate to the opposite of the current facing direction, that would be -transform.forward, not -transform.position as you're currently using in your code. Positions and directions are different things.

It looks like you want something more like this:

Quaternion targetOrientation;

void Update()
{
    if (exitSpaceShipSurroundingArea)
    {
        if (slowd) {
            SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0) 
            {
                slowd = false;                
                // Cache the 180 degree direction change once, when we start rotating.
                targetRotation = Quaternion.LookRotation(-transform.forward);
            }
        } 
        else
        {
            // Rotate toward our cached orientation.
            transform.rotation = Quaternion.RotateTowards(
                  transform.rotation,
                  targetOrientation,
                  Time.deltaTime * 180f / secondsToRotate
            );
        }
    }
}
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

If your goal is to rotate to the opposite of the current facing direction, that would be -transform.forward, not -transform.position as you're currently using in your code. Positions and directions are different things.

It looks like you want something more like this:

Quaternion targetOrientation;

void Update()
{
    if (exitSpaceShipSurroundingArea)
    {
        if (slowd) {
            SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0) 
            {
                slowd = false;                
                // Cache the 180 degree direction change once, when we start rotating.
                targetRotation = Quaternion.LookRotation(-transform.forward);
            }
        } 
        else
        {
            // Rotate toward our cached orientation.
            transform.rotation = Quaternion.RotateTowards(
                  transform.Rotation,
                  targetOrientation,
                  Time.deltaTime * 180f / secondsToRotate
            );
        }
    }
}