Unity C#理解克隆不预制的参考

时间:2017-05-01 05:32:49

标签: c# unity3d

我知道这个问题之前曾被问过10,000次。但我似乎无法掌握答案。我现在已经在Unity中构建了一些原型游戏设计,并且正在学习使用Visual Studios自动完成的TON以找出所做的事情。
   所以我很难掌握预制代码的东西。为了快速测试发布在这里,我建立了一个磁铁,当“#Control; Left Control"按下并放下释放。问题是,当我在场景中放下一些时,磁铁会选择一个随机版本的预制件。如何引用附加到脚本的单个游戏对象与预制件。

添加清晰度

所以这是主要问题。磁铁将拾取标记的任何随机物体" Car"它不会拾取触发区域内的那个。 我怎样才能让它成为'#34; Car"触发器内部?

这是磁铁代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//This Goes on the Object that is to be moved by a magnet
public class CraneMagnet : MonoBehaviour
{

    private CraneTrigger craneTrigger;
    public string getTag = "";
    void Start()
    {
        craneTrigger = FindObjectOfType<CraneTrigger>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == getTag)
        {
            craneTrigger.canPickup = true;
        }

    }
}

这是磁铁拾取的物体

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

public class TESTObjectMovedByTag : MonoBehaviour {
    public bool liftObject = false;
    Rigidbody objectRigidbody;

    // strings for get objects of type to declare in the inspector;
    public string getTag = "";

    void Start()
    {
        objectRigidbody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        PickUpObject();
    }

    void PickUpObject()
    {
        if (liftObject)
        {
            objectRigidbody.isKinematic = true;// so the gravity will affect the object
            transform.parent = GameObject.FindGameObjectWithTag(getTag).transform;// make the object child of the magnet 
        }

        if(!liftObject)
        {
            objectRigidbody.isKinematic = false;// so gravity will not affect the object
            transform.parent = null;// remove the magnet parent
        }
     }
}

1 个答案:

答案 0 :(得分:0)

我在这个例子中想出了如何只使用CraneMagnet脚本来实现这一点 对于任何感兴趣的人来说,是从不同的对象中获取标记的Rigidbody的代码

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

public class CraneMagnet : MonoBehaviour
    {
    private CraneTrigger craneTrigger;// This is calling only for controls
    public string getTag = "";// Define the tag in the inpector
    private Rigidbody objectPickedUp;
    [HideInInspector] public bool canLift = false;
    [HideInInspector] private bool isLifted = false;// only exists so the objectPickedUp cant be called until is exists 
    void Start()
    {
        craneTrigger = FindObjectOfType<CraneTrigger>();
    }

    void Update()
    {
        //call PickUpObject() in the control script
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == getTag)
        {
            objectPickedUp = other.attachedRigidbody;// make the triggered RididBody become objectPIckedUP 
            craneTrigger.canPickup = true;
        }

    }
    public void PickUpObject()
    {
        if (canLift)
        {
            objectPickedUp.transform.parent = transform;// object becomes child of magnet 
            isLifted = true;
            objectPickedUp.isKinematic = true;// so object can be moved easier
        }
        if (!canLift && isLifted)
        {
            objectPickedUp.isKinematic = false;// so gravity will take over the fall
            objectPickedUp.transform.parent = null;//the picked up object looses magnet parent
            isLifted = false;
        }
    }
}