Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

detectar velocidad de un rigidbody o un gameobject

Discussion in 'Physics' started by combra, Feb 25, 2021.

  1. combra

    combra

    Joined:
    Nov 27, 2019
    Posts:
    12
    Buenas, estoy intentando asignar una velocidad a una animación pero al iniciar, al estar ya establecida la velocidad fija, la animación esta siempre en marcha, habría alguna forma de detectar a que velocidad se mueve un objeto? para así que solo cuando se mueva ejecute la animación.
    en el script esta así:
    si le ven una solución fácil se lo agradecería con todo el corazón XD

    script1.png script2.png
     
  2. whitexroft

    whitexroft

    Joined:
    Oct 22, 2012
    Posts:
    48
    The easiest is to cache your current position and compare it with the previous frame
    So if the object hits a wall, you will see that.

    Code (CSharp):
    1. Vector3 m_prevPosition;
    2.  
    3. void Start()
    4. {
    5.     m_prevPosition = transform.position;
    6. }
    7.  
    8. void Update()
    9. {
    10.     var actualVelocity = Vector3.Distance(m_prevPosition, transform.position);
    11.     actualVelocity / = Time.deltaTime;
    12.  
    13.     m_prevPosition = transform.position;
    14. }
     
    mindalvaro and ivan2810 like this.