尝试执行操作会直接跳到另一个操作

时间:2017-08-25 20:06:36

标签: c# debugging unity3d

首先,我知道我应该在Unity论坛上提出这个问题,但我已经问了两次,没有人有答案。

现在有了这个,这是我为拾取和保存对象而编写的代码。我的问题是,现在每当我尝试拿起一个物体时,它就会直接进入掉落动作。 我认为问题始于void Update中第42行的if语句。

感谢任何帮助。

using UnityEngine;
using System.Collections;

#pragma warning disable 0414

namespace CarryingItems {

public class PickUp : MonoBehaviour {

// publics

    public bool NearObject;

    // Where to place the item.
        private GameObject Hand;

    // The object I'm carrying.
        public static GameObject CarriedObject;

        public LayerMask RayMask;

// privates
    // The parent.
        private GameObject UtilityProps;

    private Rigidbody CORB;

    private bool ItemPickedUp; 

    private float OffsetDropPosition = 2;
    private RaycastHit FilterHit;

void Awake() {
    UtilityProps = GameObject.Find("UtilityProps");
    Hand = GameObject.Find("ItemHand");
}

void Update() {

    NearObject = PickUpSphereCasts.NearObject;

    if (Input.GetKeyDown(KeyCode.E)){
        if (CarriedObject != null) {
                ItemDrop();
        }
        else if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out FilterHit, RayMask.value)){
            ItemPickUp(FilterHit.collider.gameObject);
            }
    }
}

 void ItemPickUp(GameObject objectToPickup) {
    // unless you've set your LayerMask to only hit the objects that can be picked up (you should do this as well, for optimization purposes), you must check if you can pick up this object
        if (objectToPickup.layer != 8) { // if you can't pick it up, do nothing then leave this function
            return;
        }

    if(NearObject == true){
        // from now on we know we are holding an item
            ItemPickedUp = true; 

        // make the scene modifications 
            CarriedObject = objectToPickup;

            CORB = CarriedObject.GetComponent<Rigidbody>();

            CarriedObject.transform.SetParent(Hand.transform, false);

            CarriedObject.GetComponent<Rigidbody>().useGravity = false;
            CarriedObject.GetComponent<Rigidbody>().isKinematic = true;

            CarriedObject.transform.position = Hand.transform.position;
            CarriedObject.transform.rotation = Hand.transform.rotation;

            CarriedObject.gameObject.GetComponent<Collider>().enabled = false;
    }
}

 void ItemDrop() {
    // from now on, we do not have an object picked up
        ItemPickedUp = false;

    // make the scene modifications     
        CarriedObject.transform.SetParent(UtilityProps.transform, false);

        CarriedObject.transform.position = Camera.main.transform.position + Camera.main.transform.forward * OffsetDropPosition;
        CarriedObject.transform.rotation = Hand.transform.rotation;

        CarriedObject.GetComponent<Rigidbody>().useGravity = true;
        CarriedObject.GetComponent<Rigidbody>().isKinematic = false;

        CarriedObject.gameObject.GetComponent<Collider>().enabled = true;

    // clear the reference             
        CarriedObject = null;
    }
}
}

1 个答案:

答案 0 :(得分:0)

export class AdpDetailPage{ constructor(public navParams: NavParams){ errorEvent = this.navParams.get('errorEvent'); console.log("errorEvent= ", errorEvent); } } 类中创建一个私有变量careAboutInput,并将其用作PickUp函数中的开关:

Update