- Home /
My "Lava " script doesnt work
I want the player to teleport when touching lava. Here is my script i have applied to the lava using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Lava : MonoBehaviour
{
public Transform player;
public Transform spawn;
void OnTriggerStay ()
{
player.position = spawn.position;
}
}
If i use OnTriggerEnter it doesn't do anything. If i have it at OnTriggerStay my character freezes in place and can't move. I have isTrigger enabled.
Are you sure that your spawn and player objects are correctly assigned in the Unity Editor and that the Spawn is in the correct position?
Answer by tobster56 · May 27, 2020 at 04:49 PM
I'm not that good of a coder so this might not work. but maybe using on Trigger enter would make more sense and use debug.log("lava") to see if the collision detection is the problem or 'player.position = spawn.position'
Answer by clark2001 · May 28, 2020 at 02:01 AM
This code should work.
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Lava")
{
player.position = spawn.position;
}
}