How do I warp the player?

by R3Ked   Last Updated June 12, 2019 23:13 PM

I'm new to unity, and I have two problems.

  1. I need to make an invisible area so that, when touched, the player warps back to the start.
  2. I need to make a cube that's when touched, it takes the player to a place that shows the player that they won.
Tags : unity


Answers 1


These can both be accomplished using Unity's box colliders.

  1. First, Create your cube object. Expand the box collider options in the inspector and select 'Is Trigger.' Now object will no longer just collide with the object, they will pass through it and your code can then detect the collision.
  2. Next, add a script to your trigger cube. Click the 'add component' button at the foot of the inspector and click the 'new script' option and name it whatever you like.
  3. Add the OnTriggerEnter method to execute code when the object collides with the box collider. Then you can warp the player using this code where you replace x, y, and z with the actual coordinates you want the player to be moved to.
    public class <your script name> : MonoBehaviour
    {
        // OnTriggerEnter is called when your rigid body enters the cube
        void OnTriggerEnter(Collider other) {
            // Transform (move) player's position to x, y, and z (Remember to set these values)
            Transform.position = new Vector3(x, y, z);
        }
    }

Now you just need to make two of these. One that takes the player to the end goal, and one that takes them back to the start.

One final note, make sure any game object you want to collide with these triggers have a Rigid Body or else it will not trigger the script

SeeBeyondMusic
SeeBeyondMusic
June 13, 2019 23:04 PM

Related Questions


Updated July 02, 2019 11:13 AM

Updated July 12, 2015 13:05 PM

Updated July 13, 2015 13:05 PM