Now the object rotate and only when the object finished rotating he start moving and I want that when the object is rotating while rotating start also move.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DetectCollision : MonoBehaviour
{
public Transform player;
public Transform target;
public Text currPosDistanceUiText;
public bool rotateAutomatic = false;
public bool turnOnOffPlayerAnimator = false;
float timeElapsed = 0;
float lerpDuration = 3;
float startValue = 1;
float endValue = 0;
float valueToLerp = 0;
private Animator playerAnimator;
private bool entered = false;
private bool prevFacing = false;
private bool stopped = false;
private bool move = true;
private Vector3 currPos;
// Start is called before the first frame update
void Start()
{
playerAnimator = player.GetComponent<Animator>();
if (turnOnOffPlayerAnimator)
playerAnimator.enabled = false;
}
// Update is called once per frame
void Update()
{
var currFacing = IsFacing(target);
if (currFacing != prevFacing)
{
// here you switched from facing to not facing or vise verca.
timeElapsed = 0;
}
prevFacing = currFacing;
var distance = Vector3.Distance(player.position, target.position);
if (IsFacing(target))
{
if (entered && distance > 30 && move)
{
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
playerAnimator.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
playerAnimator.SetFloat("Forward", valueToLerp);
stopped = true;
valueToLerp = 0;
}
if(move == false)
{
playerAnimator.SetFloat("Forward", 0);
}
if (playerAnimator.GetFloat("Forward") == 0 && stopped)
{
move = false;
currPos = player.position;
StartCoroutine(AnimateRotationTowards(player, Quaternion.Euler(0,90,0), 1f));
Debug.Log("Player current position when valueToLerp value is 0 : " + currPos);
}
}
else
{
if (rotateAutomatic)
{
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
playerAnimator.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
playerAnimator.SetFloat("Forward", valueToLerp);
}
else
{
if (valueToLerp < 0.9f)
{
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
playerAnimator.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
playerAnimator.SetFloat("Forward", valueToLerp);
}
}
var dist = Vector3.Distance(player.position, currPos);
currPosDistanceUiText.text = dist.ToString();
if (dist > 2)
{
move = true;
}
}
if(turnOnOffPlayerAnimator)
{
playerAnimator.enabled = false;
}
else
{
playerAnimator.enabled = true;
}
}
private void OnTriggerEnter(Collider other)
{
entered = true;
Debug.Log("Entered !");
}
private void OnTriggerExit(Collider other)
{
entered = false;
Debug.Log("Exited !");
}
private bool IsFacing(Transform target)
{
Vector3 forward = player.TransformDirection(Vector3.forward);
Vector3 toTarget = target.position - player.position;
return Vector3.Dot(forward, toTarget) > 0;
}
private System.Collections.IEnumerator AnimateRotationTowards(Transform target, Quaternion rot, float dur)
{
rotateAutomatic = true;
float t = 0f;
Quaternion start = target.rotation;
while (t < dur)
{
target.rotation = Quaternion.Slerp(start, rot, t / dur);
yield return null;
t += Time.deltaTime;
}
target.rotation = rot;
}
}
At the bottom I’m using IEnumerator and rotating the object.
I’m also setting a flag to true rotateAutomatic.
Then I’m checking here if rotateAtuomatic is true start moving the object :
if (rotateAutomatic)
{
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
playerAnimator.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
playerAnimator.SetFloat("Forward", valueToLerp);
}
I used a break point and I see it’s getting inside and doing the code inside the if (timeElapsed < lerpDuration) but then when making continue the object is not moving and start moving only when the rotating is finished.