我的标签在以一定的力或速度碰撞后如何激活\启用和对象(VR游戏-UNITY 3D)

问题描述 投票:1回答:1

我正在开发VR游戏。如果是格斗游戏,玩家将能够向敌人猛击(拳头上将是对撞机和带有损坏脚本的残障对象)。我需要一个脚本来激活另一个对象(一个带有损坏脚本的对象)-(放在他的拳头上)但只是以一定的速度或力量(就像在现实生活中一样)-如果敌人将被他的手触摸不会受到损坏,只是在较高的力量或速度下)最好的解决方案是什么?谢谢!

c# unity3d
1个回答
0
投票

由于玩家的拳头不受物理系统的控制,因此无法像正常的刚体一样读取玩家的手的速度。话虽如此,您仍然可以在一个脚本中计算速度并处理您希望执行的所有操作。

这里是一个例子:

[RequireComponent(typeof(DamageScript))]
public class HandSpeedMonitor : Monobehaviour
{
    public float threshold;
    DamageScript damageScript;
    Vector3 lastPos;

    public void Awake()
    {
        damageScript = this.GetComponent<DamageScript>();
    }

    public void Start()
    {
        lastPos = this.transform.position;
    }

    public void Update()
    {
        float velocity = (lastPos - this.transform.position).magnitude / Time.deltaTime;
        if(!damageScript.enabled && velocity > threshold)
            damageScript.enabled = true;
        else if(damageScript.enabled)
            damageScript.enabled = false;
    }
}

但是,由于.magnitude是一个昂贵的调用,因此您可能要考虑将“ threshold”存储为平方速度“ sqrThreshold”并使用.sqrMagnitude,因为它删除了矢量数学的平方根分量(节省了处理时间)。

© www.soinside.com 2019 - 2024. All rights reserved.