【问题标题】:SteamVR GameObject rigidbody released from grab with zero velocitySteamVR GameObject 刚体以零速度从抓取中释放
【发布时间】:2017-07-06 17:49:59
【问题描述】:

我的右侧 Vive 控制器上有一个触发碰撞器,它可以检测带有“Enemy”标签的游戏​​对象何时在抓取范围内。为了创建一个“抓取”,我将一个 FixedJoint 组件添加到控制器,其中 Enemy 游戏对象为 connectedBody。松开握把后,我设置了connectedBody = null 和Destroy(joint)(其中joint 是FixedJoint)。每当我释放selectedObj 时,它都会以零速度落到地上。什么给了?

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

public class Grab : MonoBehaviour {
    private GameObject selectedObj;
    private GameObject grabbableEnemy;
    public Transform gripTransform;
    private SteamVR_TrackedObject trackedObject;
    private SteamVR_Controller.Device _controllerDevice;
    private SteamVR_TrackedController _controller;

    private Vector3[] positions = new Vector3[2]; // positions[0] last frame, positions[1] this frame;
    private Vector3 releasedVelocity;

    void Awake()
    {
        trackedObject = GetComponent<SteamVR_TrackedObject>();
    }

    // Use this for initialization
    void Start () {
        _controllerDevice = SteamVR_Controller.Input((int)trackedObject.index);
        _controller = GetComponent<SteamVR_TrackedController>();
        positions[0] = Vector3.zero;
        positions[1] = Vector3.zero;
        releasedVelocity = new Vector3(-99,-99,-99);

        selectedObj = null;
        grabbableEnemy = null;
    }

    // Update is called once per frame
    void FixedUpdate () {
        if (selectedObj)
        {
            Debug.Log("Updating velocity");
            positions[0] = positions[1];
            positions[1] = selectedObj.transform.position;
            Debug.Log("Selected obj velocity: " + selectedObj.GetComponent<Rigidbody>().velocity);
        }
        if (_controllerDevice.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
        {
            Debug.Log("Gripped");
            if (grabbableEnemy)
            {
                Debug.Log("Grab sequence begins");
                selectedObj = grabbableEnemy;
                Debug.Log(selectedObj.name);
                selectedObj.transform.position = gripTransform.position;
                var joint = gameObject.AddComponent<FixedJoint>();
                joint.connectedBody = selectedObj.GetComponent<Rigidbody>();
            }
        }
        if (_controllerDevice.GetPressUp(SteamVR_Controller.ButtonMask.Grip))
        {
            Debug.Log("Ungripped");
            if (gameObject.GetComponent<FixedJoint>() && selectedObj)
            {
                Debug.Log("Launch sequence begins");
                // currently unused velocity value
                releasedVelocity = (positions[1] - positions[0]) / Time.deltaTime;

                foreach (FixedJoint joint in gameObject.GetComponents<FixedJoint>()) {
                    joint.connectedBody = null;
                    Destroy(joint);
                }
                selectedObj.GetComponent<Rigidbody>().velocity = gameObject.GetComponent<Rigidbody>().velocity;
                selectedObj.GetComponent<Rigidbody>().angularVelocity = gameObject.GetComponent<Rigidbody>().angularVelocity;

                Debug.Log("Released @ release: " + selectedObj.GetComponent<Rigidbody>().velocity);

                // For garbage collection?
                selectedObj = null;      
            }
        }
    }

    private void OnTriggerStay(Collider other)
    {
        Debug.Log("Triggered");
        if (other.gameObject.tag == "Enemy")
        {
            grabbableEnemy = other.gameObject;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        grabbableEnemy = null;
    }
}

【问题讨论】:

  • 顺便说一下,selectedObj = null; 对 GC 没有帮助。该对象仍然存在。但这仍然很好,因为脚本应该“忘记”抛出的对象。但是内存完全不受=null 的影响。但是,如果它是一个局部变量,它无论如何都会被 GC 处理。

标签: unity3d rigid-bodies htc-vive


【解决方案1】:

您正在计算releasedVelocity,但从未设置任何东西来使用它的值。

你有:

selectedObj.GetComponent<Rigidbody>().velocity = gameObject.GetComponent<Rigidbody>().velocity;

但这只会将速度设置为您的脚本所附加到的GameObject 的速度。我猜你想这样做:

selectedObj.GetComponent<Rigidbody>().velocity = releasedVelocity;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多