使用C#和Unity禁用特定游戏对象上的刚体

时间:2017-02-03 14:26:19

标签: c# unity5

我在我的捏代码中遇到了麻烦,因为它也抓住了我的播放器。这是我的代码:

using UnityEngine;
using System.Collections;
using Leap;
using Leap.Unity;

/** 
* Detects pinches and grabs the closest rigidbody if it's within a given range.
* 
* Attach this script to the physics hand object assinged to the HandController in a scene.
*/
public class MagneticPinch : MonoBehaviour {

public const float TRIGGER_DISTANCE_RATIO = 0.7f;

/** The stiffness of the spring force used to move the object toward the hand. */
public float forceSpringConstant = 100.0f;
/** The maximum range at which an object can be picked up.*/
public float magnetDistance = 0.5f;

protected bool pinching_;
protected Collider grabbed_;
public GameObject TrashAudio;

public GameObject myPlayer;

void Start() {
    pinching_ = false;
    grabbed_ = null;

}

/** Finds an object to grab and grabs it. */
void OnPinch(Vector3 pinch_position) {
        pinching_ = true;

    // Check if we pinched a movable object and grab the closest one that's not part of the hand.
    Collider[] close_things = Physics.OverlapSphere(pinch_position, magnetDistance);
    Vector3 distance = new Vector3(magnetDistance, 0.0f, 0.0f);

    for (int j = 0; j < close_things.Length; ++j) {
        Vector3 new_distance = pinch_position - close_things[j].transform.position;
        if (close_things[j].GetComponent<Rigidbody>() != null && new_distance.magnitude < distance.magnitude &&
            !close_things[j].transform.IsChildOf(transform)) {
            grabbed_ = close_things[j];
            distance = new_distance;

        }
    }
}
/** Clears the pinch state. */
void OnRelease() {
    grabbed_ = null;
    pinching_ = false;
    TrashAudio.gameObject.SetActive (false);
    //ActivateRigidbody ();
}

/**
* Checks whether the hand is pinching and updates the position of the pinched object.
*/
 void Update() {
    bool trigger_pinch = false;
    HandModel hand_model = GetComponent<HandModel>();
    Hand leap_hand = hand_model.GetLeapHand();

    if (leap_hand == null)
        return;

    // Scale trigger distance by thumb proximal bone length.
    Vector leap_thumb_tip = leap_hand.Fingers[0].TipPosition;
    float proximal_length = leap_hand.Fingers[0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;
    float trigger_distance = proximal_length * TRIGGER_DISTANCE_RATIO;

    // Check thumb tip distance to joints on all other fingers.
    // If it's close enough, start pinching.
    for (int i = 1; i < HandModel.NUM_FINGERS && !trigger_pinch; ++i) {
        Finger finger = leap_hand.Fingers[i];

        for (int j = 0; j < FingerModel.NUM_BONES && !trigger_pinch; ++j) {
            Vector leap_joint_position = finger.Bone((Bone.BoneType)j).NextJoint;
            if (leap_joint_position.DistanceTo(leap_thumb_tip) < trigger_distance)
                trigger_pinch = true;
        }
    }

    Vector3 pinch_position = hand_model.fingers[0].GetTipPosition();

    // Only change state if it's different.
    if (trigger_pinch && !pinching_)
        OnPinch(pinch_position);
    else if (!trigger_pinch && pinching_)
        OnRelease();

    //this line of code is a self scripting for disabling the rigidbody from the player component
    // Accelerate what we are grabbing toward the pinch.
    if (grabbed_ != null) {
            Vector3 distance = pinch_position - grabbed_.transform.position;
        //DeactivateRigidbody ();   //this is not to grab the rigidbody of the player
            grabbed_.GetComponent<Rigidbody> ().AddForce (forceSpringConstant * distance);
            TrashAudio.gameObject.SetActive (true);
            //DeactivateRigidbody ();
    }
}
}

每个物体都必须有一个设置为 USE GRAVITY 的刚体,所以我的玩家也不能穿过我的地形。我的脚本抓住了最近的具有刚体的对象。问题是,当我抓住一些物体时,它总是抓住自己,我的意思是我的玩家。我正在使用跳跃动作抓住。

那么如果我的脚本检测到它正在抓住我的播放器它会停止抓取而是抓住对象,我怎么能获得呢?

3 个答案:

答案 0 :(得分:0)

我的Unity有点生锈,但您可以将标记附加到GameObject,https://docs.unity3d.com/ScriptReference/GameObject-tag.html

请务必将播放器标记为播放器,之后您可以检查最近的gameobjects标签是否不是播放器&#34;。

答案 1 :(得分:0)

真的很容易。 您可以像播放器一样标记您的播放器:enter image description here

然后将类似的代码添加到方法OnPinch

 // Check if we pinched a movable object and grab the closest one that's not part of the hand.
Collider[] close_things = Physics.OverlapSphere(pinch_position, magnetDistance);
Vector3 distance = new Vector3(magnetDistance, 0.0f, 0.0f);

for (int j = 0; j < close_things.Length; ++j) {
 if(close_things[j].gameobject.Tag.ToUppercase() == "PLAYER")
    continue; //here you jump to a the next object

PS:或者您可以简单地使用忽略播放器的其他图层

答案 2 :(得分:0)

通过使用 //将此脚本附加到捏合      void DeactivateRigid(){

 myPlayer.Getcomponent<Rigidbody>().isKenimatic == false;
 myPlayer.Getcomponent<Rigidbody().detectCollision = true;
 }

//将此脚本附加到发布

 void ActivateRigid(){
 ...
 }