- Home /
Moving first person controller via script
Hello, I'm new to using unity and am looking forward to getting my hands dirty, but first I'm following along with a short tutorial series.
I'm at a point where I'm writing a small script "catch" a player when they fall off the edge of the terrain, and move them to the starting location. However, the script is not actually moving the player.
Here's the code (javascript):
function Update ()
{
if ( transform.position.y < -50 ) {
transform.position.y = 2;
transform.position.x = 0;
transform.position.z = 0;
//Send verification to console
print ( "Player fell, resetting to start." );
}
}
When I hit play and run the player off the edge, I can see the code firing off in console, but I just keep falling. I've attached the script to the player fab and can see it in inspector when I click the player. Can anyone see what I've done wrong?
Are you using the standard First Person Controller which comes with Unity? I mean the Standard Assets/Character Controllers/First Person Controller.
Answer by aldonaletto · Jul 20, 2011 at 10:52 PM
There's nothing wrong with your code. I've just tested it in a project which uses the First Person Controller, and it worked as expected. But I tested a hypothesis: I attached the script to Graphics, a First Person Controller's child, and the player fell continuously, while the Graphics child was "jumping" to the initial position each time the limit was reached. Could it be your problem? Check if the script is attached to the First Person Controller or to one of its children (Graphics or Main Camera).
You had it, thank you. I had it attached to the graphic, not the actual controller itself.
I made a similar mistake some time ago - it's highly frustrating, because you look to the script, change the instructions order, look again, place debug instructions, and nothing seems to make sense!
By the way, please click the "check" button below the voting thumbs in my answer to signal it solved the problem, so it can help others with similar issues.
Your answer
Follow this Question
Related Questions
Crosshair? How? 6 Answers
how do I move the 1st person controller by dragging the mouse 1 Answer
C# Crouch script problem 1 Answer
Can't get 1st person controller to stay on a platform. 2 Answers
how to use the dual joystick from the prefab and how can i put it in my first person controller? 4 Answers