简体   繁体   中英

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.

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.

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.

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.

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

For first, attach OnTriggerEnter to the TRIGGER, not a player.

For second, attach this script on portal and set spawnPoint explicitly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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