繁体   English   中英

unity transform.position无法正常工作

[英]Unity transform.position not working properly

好的,所以我弄清楚了我遇到的问题。 门户返回的位置在门户内部,当一个对象位于另一个对象内部时,它将突然出现,因此我只向门户添加了一个空对象,希望它们退出并让程序指向该位置。

我正在Unity中进行一个小“游戏”,有四个标记为PortalMain1PortalMain2PortalMain3PortalMain4 ,目标是使玩家能够进入每个门户上的对撞机并成为随机传送到其他门户之一。

我在播放器模型上有以下代码来处理触发事件:

private void OnTriggerEnter(Collider other)
{
    int PortalDestination = Random.Range(1, 4);  ///Portals index at 1 because Unity
    string Portal = "PortalMain" + PortalDestination;
    Debug.Log(Portal);
    transform.position = GameObject.Find(Portal).transform.position;
}

游戏中实际发生的情况是,当玩家与门户上的盒子碰撞器碰撞时,游戏会从1-4中正确选择一个门户(尽管似乎永远不会选择您触摸过的门),然后将您传送到那里。

除非您在门户附近而不是在其附近传送,这就是说,如果传送确实起作用,它似乎只能工作大约1/4的时间。

这是带有基础的场景层次结构的图片:

与基地的场面

这是建筑物场景的层次结构图:

建筑现场

这是带有门户的场景层次结构的图片:

门户现场

编辑#1:我重新设计了与Rigidbody一起使用的方法,但问题仍然存在。

private void OnTriggerEnter(Collider other)
{
    int PortalDestination = Random.Range(1, 4);  ///Portals index at 1 because Unity
    string Portal = "PortalMain" + PortalDestination;
    Debug.Log(Portal);
    GetComponent<Rigidbody>().isKinematic = true;
    GetComponent<Rigidbody>().position = GameObject.Find(Portal).transform.position;
    GetComponent<Rigidbody>().isKinematic = false;
} 

这是Github链接> https://github.com/LvInSaNevL/Bamboozle

首先,将OnTriggerEnter附加到TRIGGER,而不是播放器。

第二,将此脚本附加到门户网站上,并显式设置spawnPoint。

之后,您可以将该门户网站复制到所需的任何位置,并将其自身添加到可能的门户网站列表中,以便玩家退出。

public class Portal : MonoBehaviour {
    public static List<Portal> portals {get; protected set;}
    public Transform spawnPoint; // Set it in the editor
    protected bool ignoreNextTouches = false;

    private void Start () {
        if (portals == null)
            portals = new List<Portal>();
        portals.Add (this);
    }
    private void OnTriggerEnter (Collider other) {
        if (!other.gameObject.tag.Equals ("Player"))
            return;
        if (ignoreNextTouches) {
            ignoreNextTouches = false;
            return;
        }
        Portal random = GetRandomPortal (this);
        random.ignoreNextTouches = true;
        other.transform.position = random.spawnPoint.position;
    }
    private static Portal GetRandomPortal (Portal exceptThis) {
        Portal  p = exceptThis;
        while (p == exceptThis)
            p = portals[Random.Range(0,portals.Length)];
        return p;
    }
}

暂无
暂无

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

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