- Home /
Getting a player's position to move on contact with a plane
I'm super new to unity and I'm taking an intro class. I'm trying to figure out how to get my player object to move back to starting point after coming into contact with a plane which is set directly below the play area. So far I got this
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.name == "Player")
{
}
}
Answer by Zodiarc · Sep 30, 2016 at 09:57 AM
You need to pass the start position to the script and then
collider.gameObject.transform.position = startPosition
But if the plane is directly below the play area means that there is a y value which the player shouldm usually be able to get to so I would rather do something like this in the player controller:
if(this.transform.position.y < yourMinYValue) {
this.transform.position = startPosition;
}
this is going to sound dumb but how do i pass the position through the script? i tried using a variable defining it in a method but that doesn't work
The start position? Simply define it as a Vector3 in your Class' constructor (The variables you define at the top before writing any functions)
public class ResetPos : $$anonymous$$onoBehaviour
{
Vector3 startPosition = new Vector3(0, 0, 0); // Define the (x, y, z) position here
int your$$anonymous$$inYValue = -1; // Set the treshold in world space where the player gets reset
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(this.transform.position.y < your$$anonymous$$inYValue) {
this.transform.position = startPosition;
}
}
}
Your answer
Follow this Question
Related Questions
How to make enemy teleport right next to player? 4 Answers
Teleport 2D Player to another room 2 Answers
Skyrim style door teleport 0 Answers
How do I make an object teleport 2 Answers