0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerSpaceshipAreaColliding : MonoBehaviour
{
    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;

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

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

            if (playerAnimator.GetFloat("Forward") == 0)
            {
                transform.rotation = Quaternion.Inverse(transform.rotation);
            }
        }
    }

    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;
    }
}

When the player exit the collider area he slow down then stop. at this point I want the player to start rotating and start moving back inside the colliding area.

The exit and the slow down are working but the rotation is not working and not sure how to make him start moving again slowly to max speed when entering back.

The main goal is to prevent the player to leave the marked colliding area with some slow down and rotation.

For the rotation I did :

transform.rotation = Quaternion.Inverse(transform.rotation);

but it keep rotating non stop and not slowly smooth like using Lerp.

The coliider area

\$\endgroup\$
0
\$\begingroup\$

You could try using the sine function to what I think you want. You could then attach a rotation lerp to smooth it out. Here is a script for what this would look like (you have to implement this):

Quaternion a;//the start
Quaternion b;//and end point.
float period = 2f;//amount of seconds to rotate back and forth.

void Update()
{
  float cycles = Time.time / period;
  float amplitude = Mathf.Sin(Mathf.PI * 2 * cycles);
  amplitude /= 2;
  amplitude += 0.5f;
  transform.rotation = Quaternion.Slerp(a, b, amplitude);
}

Basically, you get the amount of cycles after period seconds. Then, you get a value from -1 to 1 (changing based on cycles because our use of sine). We don’t want that because Quaternion.Slerp takes a value from 0 to 1 as its third argument, so we divide by two (to get a value from -0.5 to 0.5), then add 0.5 (to get a value from 0 to 1). We the use Quaternion.Slerp to rotate between both points based on this 0 to 1 value.


\$\endgroup\$

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.