- Home /
teleportation problems (2d)
i want to make an enemy that when touched teleports the player to x0 y4 zn/a here is what I have: using UnityEngine; using System.Collections;
public class kill_player : MonoBehaviour
{
public GameObject player;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "murder")
{
player.transform.position = new Vector2(0, 4);
}
}
}
what makes it not work? and is it not the script?
Answer by Highwalker · May 10, 2016 at 06:50 AM
Make sure you have the player variable set in the inspector. Then try this.
public class kill_player : MonoBehaviour
{
public GameObject player;
void OnCollisionEnter(Collision col)
{
if (col.gameObject == player)
{
player.transform.position = new Vector2(0, 4);
}
}
}
it give this error: the type or namespace name "monobehaviour" could not be found
that is there is no error it still doesn't tp you
Try putting it on your enemy.
Also, is one of your colliders a trigger?
"script error: OnCollisionEnter2D This message parameter has to be of type:"
Okay. Let's start from scratch. Attach a 2D collider to your player and set it as a trigger. Set the enemy's tag to "Enemy". Then attach this script to your player.
public class PlayerTeleport : $$anonymous$$onoBehaviour
{
void OnTriggerEnter2D (Collider2D coll) {
if (coll.tag == "Enemy") {
transform.position = new Vector2 (0, 4);
}
}
}
2 problems one somehow it still no work also we need to allow the player to colide with other objects in the sene