1
\$\begingroup\$

At the top

public float secondsToRotate;
private float secondsSoFar = 0.0f;

Then in the Update

private Update()
{
  secondsSoFar += Time.deltaTime;
  float t = secondsSoFar / secondsToRotate;
  Vector3 lerpPoint = Vector3.Lerp(transform.position, -transform.position, t);
  transform.rotation = Quaternion.LookRotation(lerpPoint);
}

but instead of slowly smooth rotating it's just waiting X seconds then rotate too fast like rotating at once. I wanted it to rotate slowly smooth. for example, if I set the secondsToRotate to 5 so the rotation will take 5 seconds.

The full script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerSpaceshipAreaColliding : MonoBehaviour
{
    public float rotationSpeed;
    public float movingSpeed;
    public float secondsToRotate;

    private float timeElapsed = 0;
    private float lerpDuration = 3;
    private float startValue = 1;
    private float endValue = 0;
    private float valueToLerp = 0;
    private Animator playerAnimator;
    private bool exitSpaceShipSurroundingArea = false;
    private bool slowd = true;
    private float secondsSoFar = 0.0f;

    // Start is called before the first frame update
    void Start()
    {
        playerAnimator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (exitSpaceShipSurroundingArea)
        {
            if (slowd)
                SlowDown();

            if (playerAnimator.GetFloat("Forward") == 0)
            {
                slowd = false;
            }

            if (slowd == false)
            {
                secondsSoFar += Time.deltaTime;
                float t = secondsSoFar / secondsToRotate;
                Vector3 lerpPoint = Vector3.Lerp(transform.position, -transform.position, t);
                transform.rotation = Quaternion.LookRotation(lerpPoint);
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "CrashLandedShipUpDown")
        {
            exitSpaceShipSurroundingArea = false;
            Debug.Log("Entered Spaceship Area !");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "CrashLandedShipUpDown")
        {
            exitSpaceShipSurroundingArea = true;
            Debug.Log("Exited Spaceship Area !");
        }
    }

    private void SlowDown()
    {
        if (timeElapsed < lerpDuration)
        {
            valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
            playerAnimator.SetFloat("Forward", valueToLerp);
            timeElapsed += Time.deltaTime;
        }

        playerAnimator.SetFloat("Forward", valueToLerp);
        valueToLerp = 0;
    }
}

In that place in the script, I'm trying to rotate the player smooth slowly to the opposite direction. I want also to increase the "Forward" speed when it's rotating while it's rotating but for now the rotation itself is not working at all the player is not rotating.

if (slowd == false)
                {
                    secondsSoFar += Time.deltaTime;
                    float t = secondsSoFar / secondsToRotate;
                    Vector3 lerpPoint = Vector3.Lerp(transform.position, -transform.position, t);
                    transform.rotation = Quaternion.LookRotation(lerpPoint);
                }

The main goal is to create some realistic effect when the player is leaving the colliding area the player will slow down then when the player has stopped to start rotating slowly to the opposite direction and start moving by increasing the "Forward" value while rotating.

Some realistic preventing from the player to leave the colliding area. when he try to leave the area he should move back in the colliding area.

\$\endgroup\$
1
  • \$\begingroup\$ What you're doing with transform.position is extremely confusing. What are you ultimately trying to aim at? Try showing us the context in which you're using this code - what game feature does it serve? That can help us understand your intention behind this code. \$\endgroup\$ – DMGregory May 9 at 23:16
0
\$\begingroup\$

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
            );
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ The player starts rotating but then stops at about 45 degrees of rotating and then it seems to be struggling to rotate a bit more than stop. \$\endgroup\$ – Haim Hadad May 9 at 23:49
  • \$\begingroup\$ Sounds like you have another script or animation battling this one for control. We can't help you with that unless you edit your question to contain a minimal complete verifiable example that lets us reproduce the problem in a new, empty project. \$\endgroup\$ – DMGregory May 10 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.