Search Unity

Teleport on clicking an object?

Discussion in 'Scripting' started by VikingTheMad, Sep 2, 2016.

  1. VikingTheMad

    VikingTheMad

    Joined:
    Sep 1, 2016
    Posts:
    3
    So I'm trying to figure out how to click on an object and teleport to another location. I can't figure it out for the life of me, I'm not very good with this. Code I got is
    Code (JavaScript):
    1. var destination : Transform;
    2.  
    3. function OnTriggerEnter (col : Collider) {
    4.  
    5.     this.transform.position = destination.position;
    6. }
    What am I doing wrong?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Looks good superficially.

    But is the destination transform set properly?

    Is there a rigidbody on the object this script is on?

    Is the collider set to be IsTrigger?

    There's a good checklist to start with.
     
  3. VikingTheMad

    VikingTheMad

    Joined:
    Sep 1, 2016
    Posts:
    3
    Pretty sure its set properly, I have the destination as an empty game object tagged with 'destination', and set into the destination box on the script. Entrance has the default rigidbody applied, box collider I've tried both with ticked on or off both with trigger ticked on.

    I'm not sure if its set wrong, and not sure how I would fix it. Although I also realized that this would just let me walk through it, how would I can it so I can just click on it to teleport?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Some general rules.
    • A rigidbody should be on every object that is actively moving around the scene that you want to affect and be affected by the physics engine (anything that isn't static) along with a collider.
    • A collider should be set on any object you want to physically affect other objects, and a trigger placed on any object that you want to logically affect other objects without physically pushing against them.
    • The rigidbody/collider needs to come into physical contact with a collider or trigger in order to set off the OnCollision/OnTrigger functions.
    • Each rigidbody/collider affects other colliders and triggers depending on the layers in which they're placed.
    • The rules for which layers affect which other layers are determined in the Layer Collision Matrix in Edit => Project Settings => Physics.

    As you can see, "clicking" an object does not trigger anything using the physics system. What you need is a kind of input controller script- clicking a spot on the screen should raycast from the spot clicked by using the Camera's ScreenPointtoRay function, returning what object of a certain layer it comes in contact with. That object returned should then be considered the "destination" object, and a function called on the player object to teleport to that point. None of this would use the OnCollision/OnTrigger functions, however, because there's no physical contact being made between a rigidbody and a collider, just between a raycast and a collider.
     
  5. VikingTheMad

    VikingTheMad

    Joined:
    Sep 1, 2016
    Posts:
    3
    Well this is grand then. I have literally no idea how to make a raycast work with a transform. Can I get an example of code for this so I don't spend the next 8 hours trying to figure it out?
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Please note that I'm just writing this into the reply, so it's untested but should work or at least give you an idea of how this works. It's better to have an input manager that handles all click/button input and then sends the proper commands out to the player object remotely, but for the sake of simplicity you can just put the script with this Update function on the player itself.
    Code (csharp):
    1. private void Update()
    2. {
    3.     // this will return true only on the first frame that the left mouse click is pressed
    4.     if(Input.GetMouseButtonDown(0))
    5.     {
    6.         // this gets the raycast "laser" from the camera "forward" where you click
    7.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    8.  
    9.         // this will hold the information from what gets hit with the raycast
    10.         RaycastHit hitInfo;
    11.  
    12.         // replace TeleportObjects with name of layer that the "teleport" collider is set to
    13.         LayerMask hitMask = LayerMask.NameToLayer("TeleportObjects");
    14.  
    15.         // now actually do the raycast, and if it hits something it'll return "true" with the data for the hit in hitInfo
    16.         if(Physics.Raycast(ray, out hitInfo, Mathf.Infinity, hitMask.value))
    17.         {
    18.             // since we got a hit, set the target then call the function we want to run
    19.             destination = hitInfo.transform;
    20.  
    21.             TeleportToDestination();
    22.  
    23.             // alternatively, to just to make this simpler here, just do: transform.position = hitInfo.transform.position;
    24.         }
    25.     }
    26. }
     
    Last edited: Sep 2, 2016