- Home /
Transform.Position Help
I want to make like a teleport , changing the position of the first person controller . Here is my script , var person = first person controller :
var person : GameObject ;
function Update () { if (Input.GetKeyDown ("Q")) { person.transform.position.y = 1.381072 ; person.transform.position.x = 56.47888 ; person.transform.position.z = 47.47589 ; } }
Answer by ramonfr · Dec 25, 2011 at 08:49 PM
Try this: person.transform.position = Vector3( 1.381072, 56.47888, 47.47589);
Answer by Lo0NuhtiK · Dec 25, 2011 at 10:07 PM
Put this on your player (or any other object) then drag some other object from your hierarchy onto the "teleport" slot (this will be the end-location after teleporting , I'd create an empty game object to use as the teleport) ; after that, push play, hit the "T" key, and poof... magic.
var teleport : Transform ; //<--Drag object from Hierarchy into this in //the inspector panel, the location to teleport to
@HideInInspector var player : Transform ; //<--the transform of the object this is attached to
function Awake(){ player = transform ; //<--I'm guessing this is why yours wouldn't work. You put } //your person var at the top for caching, but never cached //it's value in awake() or start() ; Either that, or you had //attached your script to some other object and forgot to //drag&drop your player into the slot for 'person'
function Update(){ if(Input.GetKeyDown(KeyCode.T)){ TeleportMe() ; } }
function TeleportMe(){ player.position = teleport.position ; }
It's something screwy on your end then, because it works fine for me.
Answer by JerryCic · Dec 26, 2011 at 01:24 AM
Hi
I use c# so my approach is a little different. I have used the vector3 assignment approach rather that the individual x and y and z approach. But!!!! can you be sure that the event is fireing? Place the java equivilant to "Debug.Log("Q entered"); into your assignemnt routine and inspect the console to see if it is getting fired. Maybe a lowercase Q is being processed. If the routine is being processed then you will see a message.
That being said, Instead of polling in the Update cycle, why don't you use the onKeyDown event?
It only gets fired once when the key is pressed. Now again i say that i come from the vb.net (or c#.net) programming world and all my business apps are event driven. I am not sure if this event is available in Java.
Your answer
Follow this Question
Related Questions
How to transform position? 2 Answers
transform.position sends object to wrong position 1 Answer
transform position player teleport 0 Answers
Transform position? 2 Answers