- Home /
Force object position
I want to make a script so when the player falls off the map the scene gets reseted...For some reason Application.loadLevel doesn't work so i wanted to just change the player's position so he is on the map again.I tried using this :
#pragma strict
var X : float;
var Y : float;
var Z : float;
function Start () {
X = transform.position.x;
Y = transform.position.y;
Z = transform.position.z;
}
function Update () {
}
function OnTriggerEnter(other : Collider){
transform.position.x = X;
transform.position.y = Y;
transform.position.z = Z;
}
but it doesn't work?! Any help would be nice
Answer by amphoterik · Jun 28, 2013 at 11:41 AM
I am assuming that the player is not the trigger. I assume the player falls into the trigger. Therefore, your solution would not work. The OnTriggerEnter would need to be on the trigger object. What I usually do for this sort of thing is create an empty game object where I want the character to spawn. I then create a script similar to this (note that I am not good with JS, please understand the intention and not the strict syntax):
var spawnPoint: GameObject;
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player")
other.gameObject.transform.position = spawnPoint.transform.position;
}
The spawn point will need to be dragged over to the script component in the editor. The player will need to be given the tag "Player". Then, whenever the player falls in, they get moved to the spawn point.
I tried it and it doesn't work :/ Also ,just to clear things out,I'm using a collider at the bottom of the map so when the player falls he goes back to the start...I tried using Application.LoadLevel with your "tactic" and it still didn't work...Also the "public var" should be just "var" :D
The code should work if the trigger fires. What do you mean 'it doesn't work.' Does the trigger get hit? Is the spawnpoint correctly initialized? Put a Debug.Log() statement inside the OnTriggerEnter() to see if it is getting executed.
When i'm saying it doesn't work i mean that nothing is happening(?!)No Debug no nothing!!!
I tried it with an old Player i have and it worked so it must be something from the new one...Would "DontDestroyOnLoad()" prevent the player from changing position?
No Debug.Log() output means your trigger is not going off? Verify that your collider has ISTrigger set. Verify that your player is intersecting the collider that has the trigger script. Verify that your player has a collider. Verify that either the trigger or the player has a Rigidbody.