如何访问 OnVelocityChange 事件?

huangapple go评论65阅读模式
英文:

How can I access OnVelocityChange Event?

问题

我需要跟踪这个参数 (rigidbody.velocity),如果参数发生变化,需要执行一些操作。如何做到这一点?

英文:

I have some parameter (rigidbody.velocity)

I need to track this, and, if the parameter is changed, some action must be doing.

How to do it?

答案1

得分: 2

以下是翻译好的部分:

Your question has a very simple answer. Whenever you change this variable in your code, you can call the change event. In fact, a function similar to Property. But since your variable is rb.velocity and you want to check its change. As below, record the previous frame value and check it with the current value before resetting.

Note that velocity in 2D is Vector2D.

private Vector3 lastVelocity; // 缓存上一帧的 rb.velocity
public void Update()
{
    if (rb.velocity != lastVelocity)
    {
        OnVeloictyChange(rb.velocity - lastVelocity);
    }
    
    lastVelocity = rb.velocity;
}

private void OnVeloictyChange(Vector3 delta)
{
     // 值在本帧中已更改,因此事件代码在此处
}
英文:

Your question has a very simple answer. Whenever you change this variable in your code, you can call the change event. In fact, a function similar to Property. But since your variable is rb.velocity and you want to check its change. As below, record the previous frame value and check it with the current value before resetting.

Note that velocity in 2D is Vector2D.

private Vector3 lastVelocity; // cache previous frame rb.velocity
public void Update()
{
    if (rb.velocity != lastVelocity)
    {
        OnVeloictyChange(rb.velocity-lastVelocity);
    }
    
    lastVelocity = rb.velocity;
}

private void OnVeloictyChange(Vector3 delta)
{
     // value is changed This Frame, So Event code is here
}

答案2

得分: 0

以下是代码部分的中文翻译:

由于这是第三方库的属性(在这种情况下是Unity),您不能简单地添加跟踪它的内容,您可以创建一个层,将信息传递给刚体,并在执行前或执行后触发所需的事件,类似于这样。然后,您需要确保所有速度的更改都通过这个类传递。

[RequireComponent(typeof(Rigidbody))]
public class MyClass : MonoBehaviour
{
    private new Rigidbody rigidbody;
    private Rigidbody Rigidbody => rigidbody == null
        ? rigidbody = GetComponent<Rigidbody>()
        : rigidbody;

    public event Action<(Vector3 oldValue, Vector3 newValue)> OnChangingRigidbodyVelocity;
    public event Action<Vector3> OnChangedRigidbodyVelocity;
    
    public Vector3 Velocity
    {
        get => Rigidbody.velocity;
        set
        {
            var oldValue = Rigidbody.velocity;
            OnChangingRigidbodyVelocity?.Invoke((oldValue, value));
            Rigidbody.velocity = value;
            OnChangedRigidbodyVelocity?.Invoke(value);
        }
    }
}
英文:

As this is a property of a third library (in this case unity) you can't simply add something to track it, what you can do instead is create a layer that will pass the information to a rigid body and trigger the events that you wish before or after doing it, something on this line. Then u need to make sure that all changes in velocity pass through this class

    [RequireComponent(typeof(Rigidbody))]
    public class MyClass : MonoBehaviour
    {
        private new Rigidbody rigidbody;
        private Rigidbody Rigidbody =&gt; rigidbody == null
            ? rigidbody = GetComponent&lt;Rigidbody&gt;()
            : rigidbody;

        public event Action&lt;(Vector3 oldValue, Vector3 newValue)&gt; OnChangingRigidbodyVelocity;
        public event Action&lt;Vector3&gt; OnChangedRigidbodyVelocity;
        
        public Vector3 Velocity
        {
            get =&gt; Rigidbody.velocity;
            set
            {
                var oldValue = Rigidbody.velocity;
                OnChangingRigidbodyVelocity?.Invoke((oldValue, value));
                Rigidbody.velocity = value;
                OnChangedRigidbodyVelocity?.Invoke(value);
            }
        }
    }

huangapple
  • 本文由 发表于 2023年4月1日 00:33:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定