0
\$\begingroup\$
void Update()
    {
        var vel = rigidbody.velocity;
        movementSpeedUiText.text = vel.ToString();

The result is :

Speed

what I understand from this ? what is the speed ? I don't want to know only if the player is moving or not but what is the speed. what I see is some vector3.

This is the complete script :

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;

    private float distance;
    private Rigidbody rigidbody;
    private Vector3 lastPosition;
    private bool isMoving;

    // Start is called before the first frame update
    void Start()
    {
        lastPosition = transform.position;
        isMoving = false;

        rigidbody = GetComponent<Rigidbody>();

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

    // Update is called once per frame
    void Update()
    {
        if (isMoving == false)
        {
            movementSpeedUiText.text = "0";
        }
        else
        {
            movementSpeedUiText.text = animator.GetFloat("Forward").ToString();
        }
        distanceFormTargetUiText.text = Vector3.Distance(transform.position, target.transform.position).ToString();
        playerDirectionUiText.text = transform.forward.ToString();

        if (transform.position != lastPosition)
            isMoving = true;
        else
            isMoving = false;

        lastPosition = transform.position;
    }
}

because the player have animator and also some more animators on the childs the movement speed is affected by this animators. even if the player is not moving he is still looks like moving when calculating the speed.

"Forward" is parameter of the HumanoidWalk speed so when I press the W key and the player is moving it's showing the value of the "Forward" parameter.

and because when the player is not moving I don't press on W or any other key it's showing in the movementSpeedUiText text some E long number. That's why I added the part that detect if the player is moving or not and if not I assign "0" to the text.

The problem now is when I press on the W key and move the player the text is stuttering flickering.

\$\endgroup\$
3
  • \$\begingroup\$ A velocity in Unity is units per second. So this depends on what you think your distance unit is. \$\endgroup\$
    – Vaillancourt
    Mar 22 at 22:55
  • \$\begingroup\$ ...and of course, the speed is the length of the velocity vector. \$\endgroup\$
    – Vaillancourt
    Mar 22 at 23:01
  • \$\begingroup\$ @Vaillancourt Edited my question what I tried to do. \$\endgroup\$ Mar 22 at 23:14
1
\$\begingroup\$

Sounds like you want something like...

velocity.magnitude.ToString("F1");

This takes the vector velocity, converts it to a scalar speed, and expresses it as a text string in "Fixed-point" notation ("F") with one digit after the decimal ("1").

See Standard Numeric Format Strings for more ways you can control how the value is represented in text.

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