- Home /
How to teleport a character to a given position?
I'm creating a real-time tour simulation. And would like the first person controller to teleport to a new position on contact with certain "Gate"s. However I found that the normally used transform.position=(x,x,x) doesn't work.
I tried controller=GetComponent(CharacterController) and then use controller.Move(), this works fine but can only transport the character for a certain distance(i.e. only relative position), but wouldn't go to the absolute position with respect to the world as I expect. Moreover, this Move() function is also affected by the character's speed.
I also tried something like:
var Position_elevator:Vector3=new Vector3(-110,0,20);
function OnControllerColliderHit (hit:ControllerColliderHit){
var transform_position:Vector3;
if(hit.transform.name=="Cube_CorridortoElevator")
transform_position=Position_elevator;
transform.position=transform_position;
} //or, //transform.position.Set(transform_position.x,transform_position.y,transform_position.z) //which still doesn't work...
Is there any good way to change the character's position abruptly to a given position? Thank you very much!
what is Position_elevator is it defined in the begining
Try using this
if(hit.transform.name=="Cube_CorridortoElevator")
transform_position = hit.transform.poistion;
transform.position=transform_position;
Answer by Cains · Nov 10, 2013 at 05:07 AM
The below should work (as long as this is attached to your character controller), if it doesn't tell me how it doesn't work.
var Position_elevator : Vector3 = new Vector3(-110,0,20);
function OnControllerColliderHit (hit:ControllerColliderHit){
if(hit.transform.name=="Cube_CorridortoElevator") transform.position = Position_elevator;
}
thanks a lot! The simplest solution is the best!! I wonder why had I taken all the trouble to circumvent this...
Your answer
Follow this Question
Related Questions
AI movement using Character Controller, turning problem 1 Answer
CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer
Movement with physics 1 Answer
How to rotate the character controller? 1 Answer
How would i rewrite this script with a Character Controller? C# 1 Answer