How to make a player teleport on collision?
Hello, how would I make a player teleport somewhere if it hits a certain GameObject?
Answer by IronPig_ · May 13, 2017 at 05:24 PM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Teleport : MonoBehaviour {
public GameObject YouGameObject;
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject == YouGameObject)
{
transform.position = new Vector3(0, 0, 0);//(where you want to teleport)
}
}
}
Answer by Breeziggy · Feb 15, 2018 at 02:54 PM
@IronPig_ I have tried this script but it currently isn't working
The script isn't working, because you don't have the script in the class.
If you look above the script:
public GameObject Player;
You have there one more bracket. Just remove it and put under this script:
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject == Player)
{
transform.position = new Vector3(0,0,0);
}
}
At the top of your script named PlayerController you have probably this:
public class PlayerController : $$anonymous$$onoBehaviour{
}
Your code must be in this two brackets. I am too tryed this code but not working, then i asked on this forum. Then i send bug report to unity, because it not working only in my method. At the end solution of this is not too bad. Here is my question: Here
I hope to help you.
Your answer
Follow this Question
Related Questions
Unty 3D C# Load other scene when bool = true, otherwise perform teleportation as commanded. 1 Answer
How do I make a secret area in my game 0 Answers
OnCollisionEnter/OnControllerColliderHit/onTriggerEnter Won't work as teleporter? 0 Answers
Teleport through walls even with colliders and rigidbody 1 Answer
Making Portals 2 Answers