Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Teleport GameObject Close But Not On Screen

Discussion in 'Physics' started by davii, May 12, 2015.

  1. davii

    davii

    Joined:
    May 9, 2015
    Posts:
    25
    Is there anyway to "teleport" a gameobject on collision to somewhere else close by but not on screen (so the player doesn't see the object appear out of nowhere)?

    Here's the script I'm working with.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collisions : MonoBehaviour
    5.  
    6. {
    7.     void OnCollisionEnter (Collision col)
    8.     {
    9.         if(col.gameObject.name == "Boid(Clone)")
    10.         {
    11.             // Teleport Boid(Clone) object to somewhere close by but not on screen
    12.         }
    13.     }
    14. }
     
  2. K0lipser

    K0lipser

    Joined:
    May 12, 2015
    Posts:
    8
    Do we talk about 2D or 3D?

    "Teleport" means to me, that you immediatly switch the position of an object.

    Code (JavaScript):
    1. col.transform.position = camera.transform.position + camera.transform.forward * distance;
    This sets the position of your object in front of your camera and any distance.

    Hope I could help you.
     
    davii likes this.
  3. davii

    davii

    Joined:
    May 9, 2015
    Posts:
    25
    The games in 3D, just so ya know!

    I've tried adding what you said but my code wants to change it to this
    Code (CSharp):
    1. col.transform.position = GetComponent<Camera>().transform.position + GetComponent<Camera>().transform.forward * 100f;
    And even with the change it doesn't effect the object?

    Full code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collisions : MonoBehaviour
    5.  
    6. {
    7.     void OnCollisionEnter (Collision col)
    8.     {
    9.         if(col.gameObject.name == "Boid(Clone)")
    10.         {
    11.             col.transform.position = GetComponent<Camera>().transform.position + GetComponent<Camera>().transform.forward * 100f;
    12.         }
    13.     }
    14. }
     
  4. K0lipser

    K0lipser

    Joined:
    May 12, 2015
    Posts:
    8
    Is the script attached to the camera? Otherwise it won't work.

    "GetComponent<Camera>()" needs a camera script on this object. I don't know anything about your objects, I just gussed, that you are using a camera.

    And a distance from 100 could be much too far - check your scene view, where your object is after the assignment.

    Code (JavaScript):
    1. var camera = GameObject.Find("Main Camera");
    2.  
    3. col.gameObject.transform.position = camera.GetComponent<Camera>().transform.position+ camera.GetComponent<Camera>().transform.forward* 100f;
     
    davii likes this.
  5. davii

    davii

    Joined:
    May 9, 2015
    Posts:
    25
    Ahh of course, it needs a camera variable. fantastic, thanks for the help :)

    Here's the code for anyone with the same issue.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Collisions : MonoBehaviour
    6. {
    7.     void OnCollisionEnter (Collision col)
    8.     {
    9.         if(col.gameObject.name == "Boid(Clone)")
    10.         {
    11.             //Set the camera variable with your camera name
    12.             var camera = GameObject.Find("Camera");
    13.             //Set transform.forward as a munis to move to behind the camera
    14.             col.gameObject.transform.position = camera.GetComponent<Camera>().transform.position+ camera.GetComponent<Camera>().transform.forward * -25f;
    15.         }
    16.     }
    17. }