简体   繁体   English

将播放器对象更改为预制件

[英]Changing the player object into a Prefab

I want to create a Prop Hunt game involving transforming into an object that is encountered by the player.我想创建一个 Prop Hunt 游戏,涉及转换为玩家遇到的对象。 To do this, the player will be observing the object with a raycast from the camera, and changing is appearance to mimic the object.为此,玩家将使用来自摄像机的光线投射观察对象,并更改外观以模仿对象。 The two issues that I have are;我的两个问题是;

  1. I am unsure how to create a reference to the prefab of the game object that is being targeted.我不确定如何创建对目标游戏对象预制件的引用。
  2. I am unsure how to alter the prefab of the player.我不确定如何更改播放器的预制件。

Any help with this would be greatly appreciated.对此的任何帮助将不胜感激。 Thanks in advance.提前致谢。

Usually you wanna destroy gameObject and give its properties to player通常你想销毁游戏对象并将其属性提供给玩家

 void Update() {

    if (Input.GetKeyUp(KeyCode.E))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out var hit))
        {
            GameObject obj = hit.collider.gameObject;

            if (obj.CompareTag("prop"))
            {
                // or check that obj has needed component

                Mesh mesh = obj.GetComponent<MeshFilter>().mesh;
                GetComponent<MeshFilter>().mesh = mesh;
                //also you can copy position, rotation, bounds, hp etc
                Destroy(obj); //
            }
        }
    }
}

That is a really basic and dirty code but it probably works这是一个非常基本和肮脏的代码,但它可能有效

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

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