0
\$\begingroup\$

The problem is that the player have Animator so even if he is not moving he is still moving a little :

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

public class DetectCollision : MonoBehaviour
{
    public Transform player;
    public Transform target;
    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 Rigidbody rigidbody;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = player.GetComponent<Rigidbody>();

        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)
            {
                if (timeElapsed < lerpDuration)
                {
                    valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                    playerAnimator.SetFloat("Forward", valueToLerp);
                    timeElapsed += Time.deltaTime;
                }
                playerAnimator.SetFloat("Forward", valueToLerp);

                if(rigidbody.velocity == Vector3.zero)
                {
                    Debug.Log(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);
            }
        }

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

I tried to use the Rigidbody on the player :

if(rigidbody.velocity == Vector3.zero)
                    {
                        Debug.Log(valueToLerp);
                    }

Using a break point I see that the velocity of the rigidbody the left side is 0.0,0.0,0.0 also the Vector3.zero have the same values but it's never get inside to the Debug.Log

In another script I get the player info also the movement speed :

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

public class PlayerSpeedDistanceController : MonoBehaviour
{
    public GameObject target;
    public GameObject speedArea;
    public float speed;
    public Animator animator;
    public Text movementSpeedUiText;
    public Text distanceFormTargetUiText;
    public Text playerDirectionUiText;
    public Text playerOnSpeedAreaUiText;

    private float distance;
    private Rigidbody rigidbody;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();

        movementSpeedUiText.text = animator.GetFloat("Forward").ToString();
        distanceFormTargetUiText.text = Vector3.Distance(rigidbody.position, target.transform.position).ToString();
        playerDirectionUiText.text = transform.forward.ToString();


    }

    // Update is called once per frame
    void FixedUpdate()
    {
        var movement = rigidbody.velocity;

        movementSpeedUiText.text = movement.magnitude.ToString("0.###");

        distanceFormTargetUiText.text = Vector3.Distance(rigidbody.position, target.transform.position).ToString();
        playerDirectionUiText.text = IsFacing(target.transform).ToString();


    }

    private bool IsFacing(Transform target)
    {
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 toTarget = target.position - transform.position;
        return Vector3.Dot(forward, toTarget) > 0;
    }
}

And I see on the movementSpeedUiText UI text that when the player is standing the movement speed is moving between : 0.001 to 0.002 mostly stay on 0.002

I want to find when the player is standing. when it's 0.001 or 0.002 I see the player standing. but in the first script it's never get inside to the DEbug.Log

Debug.Log(valueToLerp);

This small movements 0.001 or 0.002 I guess they are because the player animator make the player idle his body moving a bit.

still I'm not sure how to detect then that the player is standing not moving because he is not moving I mean the player is not changing position.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Have you considered something like this?

public float stationaryTolerance = 0.005f;

public bool IsStationary { get {
    return rigidbody.velocity.sqrMagnitude < stationaryTolerance * stationaryTolerance;
}}

Or...?

Vector3 _lastPosition;
bool _isStationary;

void FixedUpdate() {
    float travelSquared = (rigidbody.position - _lastPosition).sqrMagnitude;
    _isStationary = travelSquared < stationaryTolerance * stationaryTolerance;

   _lastPosition = rigidbody.position;
}

Either way, you can adjust your stationaryTolerance value higher or lower until it properly distinguishes "still enough" from "visibly moving"

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .