简体   繁体   English

unity transform.position无法正常工作

[英]Unity transform.position not working properly

Okay so I figured out the issue I was having. 好的,所以我弄清楚了我遇到的问题。 The location that was being returned for the portal was inside the portal and it would glitch out when one object was inside another, so I just added an empty object to the portal where I want them to exit and had the program point to that. 门户返回的位置在门户内部,当一个对象位于另一个对象内部时,它将突然出现,因此我只向门户添加了一个空对象,希望它们退出并让程序指向该位置。

I am working on a little "game" in Unity and there are four portals labeled, PortalMain1 , PortalMain2 , PortalMain3 , and PortalMain4 and the goal would be for the player to be able to run into the collider on each on of the portals and be randomly teleported to one of the other portals. 我正在Unity中进行一个小“游戏”,有四个标记为PortalMain1PortalMain2PortalMain3PortalMain4 ,目标是使玩家能够进入每个门户上的对撞机并成为随机传送到其他门户之一。

I have this bit of code on the player model handling the trigger events: 我在播放器模型上有以下代码来处理触发事件:

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

What actually happens in the game is when the player collides with the box collider on the portal the game will properly choose a portal from 1-4 (although, it seems to never choose the one you touch which is neat) and teleport you there. 游戏中实际发生的情况是,当玩家与门户上的盒子碰撞器碰撞时,游戏会从1-4中正确选择一个门户(尽管似乎永远不会选择您触摸过的门),然后将您传送到那里。

Except you get teleported near the portal instead of on top of it, and that's if the teleportation does work it seems to only work about 1/4 the time. 除非您在门户附近而不是在其附近传送,这就是说,如果传送确实起作用,它似乎只能工作大约1/4的时间。

Here is a picture of the hierarchy of the scene with the base: 这是带有基础的场景层次结构的图片:

与基地的场面

Here is a picture of the hierarchy of the scene with the building: 这是建筑物场景的层次结构图:

建筑现场

Here is a picture of the hierarchy of the scene with the portal: 这是带有门户的场景层次结构的图片:

门户现场

EDIT #1: I reworked my method to work with Rigidbody , but the problem persists. 编辑#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;
} 

Here is the Github link > https://github.com/LvInSaNevL/Bamboozle 这是Github链接> https://github.com/LvInSaNevL/Bamboozle

For first, attach OnTriggerEnter to the TRIGGER, not a player. 首先,将OnTriggerEnter附加到TRIGGER,而不是播放器。

For second, attach this script on portal and set spawnPoint explicitly. 第二,将此脚本附加到门户网站上,并显式设置spawnPoint。

After that you can copy that portal anywhere you want, and it will add itself to list of possible portals for player to exit. 之后,您可以将该门户网站复制到所需的任何位置,并将其自身添加到可能的门户网站列表中,以便玩家退出。

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