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.
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\$