Script for teleport on collision?
I'm inexperienced with unity, I've only made one game that's just on the border of functional. The scripts I've written for this haven't been functional at all. I can't find a second question or tutorial that has what I need for the game. I need a script with two public boxes. The player will collide with an object (the first box that was entered, say a cube or something) and immediately be transported to the coordinates that were entered in the second box. I'm bad at explaining things. I essentially want -public, object for collision... public, coordinates of appearance- In C# if you can please, with an explanation.
Answer by Firedan1176 · Jan 04, 2016 at 02:51 AM
To detect collisions in script, Unity uses callbacks such as OnCollisionEnter(Collision)
and OnTriggerEnter(Collider)
.
Both your player and the object need to have a Collider on them.
One of the two need to have a Rigidbody. If you don't want either of them to fall, just set it to isKinematic.
Then, you simply place this snippet into a script on your player:
public GameObject cube2;
void OnCollisionEnter(Collision col) {
transform.position = col.transform.position;
}
This will work, but you can also set the Collider on your teleporting cube to "Trigger", and in the code above, replace OnCollisionEnter(Collision col)
with OnTriggerEnter(Collider col)
. Notice how one takes a Collision, and the other one takes a Collider.
Let me know if you need anything else.
Answer by Kreeture36 · Sep 13, 2016 at 08:01 AM
but isn't OnCollisionEnter in Javascript not C sharp?
Your answer
Follow this Question
Related Questions
Can't make player character teleport from one side of screen to another. 1 Answer
how to teleport an object,want to teleport an object but it won't work 1 Answer
need help teleporting an object 0 Answers
Teleport script help! 1 Answer
Struggling with creating a Slender-like AI (Creating this for educational purposes) 0 Answers