- Home /
Why the player is not rotating and moving at the same time ?
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 bool rot = false;
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;
rot = true;
currPos = player.position;
Debug.Log("Player current position when valueToLerp value is 0 : " + currPos);
}
}
else
{
if (rotateAutomatic == false)
{
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 (rotateAutomatic && rot)
{
//StartCoroutine(AnimateRotationTowards(player, Quaternion.Euler(0, 180, 0), 5f));
player.rotation = Quaternion.Lerp(player.rotation, Quaternion.Euler(0, 180, 0), Time.time * 0.0003f);
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
playerAnimator.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
playerAnimator.SetFloat("Forward", valueToLerp);
}
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;
}
}
The part in the Update that should rotate and move the player at the same time is :
if (rotateAutomatic && rot)
{
//StartCoroutine(AnimateRotationTowards(player, Quaternion.Euler(0, 180, 0), 5f));
player.rotation = Quaternion.Lerp(player.rotation, Quaternion.Euler(0, 180, 0), Time.time * 0.0003f);
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
playerAnimator.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
playerAnimator.SetFloat("Forward", valueToLerp);
}
I tried to use Coroutine then just rotation but in both cases the player is first rotating only then start moving and also when moving it's stuttering.
I want at this part that the player will start rotating and moving at the same time. Also the player rotation should be automatic to the opposite direction he is facing by 180 degrees.
Comment