繁体   English   中英

Unity C#理解参考以克隆而不是预制

[英]Unity C# understanding reference to clone not prefab

所以我知道这个问题已经被问过一万遍了。 但我似乎无法掌握答案。 我现在已经在Unity中构建了一些原型游戏设计,并且正在通过使用Visual Studios自动完成功能来学习TON,以了解功能。
因此,我很难掌握预制代码的内容。 为了在此处发布快速测试,我制作了一块磁铁,当按下“左控制”并释放时将其捡起。 问题是当我在现场放一些磁铁时,磁铁会拾取随机的预制件。 我如何引用脚本中的单个游戏对象与预制对象。

添加清晰度

所以这是主要问题。 磁铁会拾取任何标记为“ 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
        }
     }
}

在这种情况下,我想出了如何仅使用CraneMagnet脚本来实现这一点。对于感兴趣的人,这里的代码是从另一个对象中拾取标记的刚体的代码

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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM