Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Same Script working diffrently

Discussion in 'Scripting' started by moscovicinadav, Nov 26, 2022.

  1. moscovicinadav

    moscovicinadav

    Joined:
    Nov 21, 2021
    Posts:
    5
    So I have this script that prevents the player from falling out of bounds, by checking if he hits a cube underneath the map and then teleports him to a respawn point and it works perfectly fine, however if I try to apply the same script to a portal game object, switched to trigger collider to the portal collider and the respawn point to the portal exit point it no longer works, I mean it rotates the player with the rotation of the portal exit but it doesn't teleport him? why's that?
    Here's a video of the issue:

    And here's the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Portal : MonoBehaviour
    6. {
    7.     private GameObject player;
    8.     private GameObject portal;
    9.  
    10.     void Start()
    11.     {
    12.         player = GameObject.FindWithTag("Player");
    13.         portal = GameObject.FindWithTag("PortalExit");
    14.     }
    15.  
    16.     void OnControllerColliderHit(ControllerColliderHit hit)
    17.     {
    18.         if (hit.gameObject.tag == "PortalEnter")
    19.         {
    20.             player.transform.position = portal.transform.position;
    21.             player.transform.rotation = portal.transform.rotation;
    22.         }
    23.         //Debug.Log("Colliding with " + hit.collider.gameObject.name); //checks if collison is happening
    24.     }
    25. }
    26.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Is there a reason why you posted this on the C# Job sub-forum? I don't see anything related to it above.
     
    Niter88 likes this.
  3. Niter88

    Niter88

    Joined:
    Jul 24, 2019
    Posts:
    112
    It seems like you're exiting on your entering position with your rotation backwards.

    Try to reference the right exit point
     
  4. moscovicinadav

    moscovicinadav

    Joined:
    Nov 21, 2021
    Posts:
    5
    I know that’s the issue I reference the right exit point but it only changes my rotation and leaves my position the same
     
  5. Niter88

    Niter88

    Joined:
    Jul 24, 2019
    Posts:
    112
    There are many things that can be wrong there. Since you're not using SOLID patterns I can't figure a lot from your code.
    I imagine that the portal variable holds a reference to the exit portal. it would be better to use like currentPortal and exitPortal. Something to differentiate the current entering portal that holds the script from the exiting one.

    The
    portal = GameObject.FindWithTag("PortalExit")
    can make a mess if you're having multiple portals, because it will get the first gameobject with that name. That means that it can find the current portal instead, for an example.

    You need to check which variables you're reading from. If you're getting the wrong value your code will present strange behaviors.

    It's not just the script, you have to think it all right.

    Try using a script like this instead:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [RequireComponent(typeof(Rigidbody))]
    7. public class Portal : MonoBehaviour
    8. {
    9.     private GameObject player;
    10.     private GameObject currentPortal; //if you need it
    11.     [SerializeField] Transform currentExitTransform; //the current portal exit point transform
    12.  
    13.     [SerializeField] Portal exitPortal; //the other portal which you're exiting
    14.  
    15.     void Start()
    16.     {
    17.         currentPortal = this.gameObject;
    18.  
    19.         Rigidbody rb = GetComponent<Rigidbody>();
    20.         rb.useGravity = false;
    21.         rb.isKinematic = true;
    22.  
    23.         player = GameObject.FindWithTag("Player");
    24.     }
    25.  
    26.     private void OnTriggerEnter(Collider other)
    27.     {
    28.         if (other.gameObject == player)
    29.         {
    30.             other.transform.position = exitPortal.GetPosition();
    31.             other.transform.rotation = exitPortal.GetRotation();
    32.  
    33.             //same as
    34.             //player.transform.position = exitPortal.GetPosition();
    35.             //player.transform.rotation = exitPortal.GetRotation();
    36.         }
    37.     }
    38.  
    39.  
    40.     public Vector3 GetPosition()
    41.     {
    42.         return currentExitTransform.position;
    43.     }
    44.  
    45.     public Quaternion GetRotation()
    46.     {
    47.         return currentExitTransform.rotation;
    48.     }
    49. }
    Make a prefab, add this script as a component. I will add a RigidBody automatically, you have to add a colision, maybe a box collider, activate it's checkmark that says Is Trigger. Make a child empty game object in it and drag it to the exposed currentExitTransform on your portal component. Now save your prefab and go back to the scene.

    Drag two of the portal's prefab to your scene. In which one you will drag the other portal into the exitPortal variable slot on the component.

    note that the components Portal, RigidBody and the Collider have all to be on the same GameObject, they need to communicate.

    Hit play, it will magically work. If it doesn't just delete everything and follow all the steps again.

    Edit: for god's sake, make sure that your exit point transform is outside (and away from) your collider, on the front of the portal facing outwards
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,615
    Does the character have a Unity CharacterController component on it? The CharacterController also sets the players position, so in your case it might be setting the player's position back to what it was before.
     
  7. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    your portal script has
    Code (CSharp):
    1. if (hit.gameObject.tag == "PortalEnter"){
    2. ...
    3. }
    shouldnt it check if the tag is "player", since in the start method you have:
    Code (CSharp):
    1. player = GameObject.FindWithTag("Player");
    another thing to consider that happened to me with portals, is that the player properly teleported to the exit portal. then the collision between the player and the exit portal was detected, triggering a teleportation back to the entrance portal, so all that noticeably changed was rotation. worth checking out.
     
  8. moscovicinadav

    moscovicinadav

    Joined:
    Nov 21, 2021
    Posts:
    5
    Ok thanks to everyone for the help