Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#和Unity禁用特定游戏对象上的刚体_C#_Unity5 - Fatal编程技术网

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

使用C#和Unity禁用特定游戏对象上的刚体,c#,unity5,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

我的夹码有问题,因为它也抓住了我的播放器。这是我的密码:

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 ();
    }
}
}
使用UnityEngine;
使用系统集合;
使用Leap;
运用跳跃、统一;
/** 
*如果最近的刚体在给定范围内,则检测并抓住它。
* 
*将此脚本附加到场景中指定给HandController的物理手对象。
*/
公共类磁点:单一行为{
公共常数浮动触发距离比=0.7f;
/**用于将对象移向手的弹簧力的刚度*/
公共浮点数常数=100.0f;
/**可以拾取对象的最大范围*/
公共浮子磁距=0.5f;
受保护的bool-pinching;
受保护的对撞机;
公共游戏对象TrashAudio;
公共游戏对象myPlayer;
void Start(){
捏压=假;
参数=null;
}
/**找到要抓取的对象并抓取它*/
无效ON挤压(矢量3挤压位置){
捏紧=正确;
//检查我们是否捏住了一个可移动的物体,并抓住离我们最近的一个不是手的部分。
对撞机[]关闭物体=物理。重叠球体(挤压位置,磁距);
Vector3距离=新Vector3(magnetDistance,0.0f,0.0f);
for(int j=0;j
每个物体都必须有一个刚体,该刚体被设置为使用重力,这样我的玩家也可以这样,它就不会穿过我的地形。我的脚本是抓取最近的具有刚体的对象。问题是当我抓住某个物体时,它总是抓住自己,我指的是我的球员。我用跳跃动作来抓


那么,如果我的脚本检测到它正在抓取我的玩家,它将停止抓取,而是抓取该对象,我的Unity有点生锈,但你可以在GameObject上附加一个标签

确保将玩家标记为玩家,之后可以使用检查最近的gameobjects标记是否为“玩家”。

非常简单。 您可以像图像中的玩家那样标记您的玩家:

然后在方法
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
//检查我们是否捏住了一个可移动的物体并抓住了离我们最近的一个不是手的部分。
对撞机[]关闭物体=物理。重叠球体(挤压位置,磁距);
Vector3距离=新Vector3(magnetDistance,0.0f,0.0f);
for(int j=0;j
PS:或者你可以使用一个不同的图层,通过使用 //将此脚本附加到pinching void DeactivateRigid(){


尝试过但没有做任何事情仍然抓住了我的播放器尝试过但仍然抓住了我的播放器。哈哈但无论如何谢谢><有什么想法吗???你使用了遮罩过滤器来覆盖你的球体吗?我有同样的问题:)读好你的代码我相信我的解决方案会有效。如果(抓取了!=null){…}试着调试这个变量,在这里你不应该在带有“Player”标签的gameobject上安装碰撞器。
 myPlayer.Getcomponent<Rigidbody>().isKenimatic == false;
 myPlayer.Getcomponent<Rigidbody().detectCollision = true;
 }
 void ActivateRigid(){
 ...
 }