- Home /
Teleport object to other objects position
I have this script: #pragma strict
var steps;
var step1 : Transform;
var step2 : Transform;
var step3 : Transform;
var step4 : Transform;
var step5 : Transform;
var step6 : Transform;
var step7 : Transform;
var step8 : Transform;
var step9 : Transform;
var step10 : Transform;
var step11 : Transform;
var step12 : Transform;
var step13 : Transform;
var step14 : Transform;
function Start () {
}
function Update () {
steps = gameObject.Find("Board").GetComponent(Dice).steps;
if(Input.GetKey("t")) {
transform.position = step1;
}
}
The step variable contains some numbers. What i want is this script is when the variabel "step" is for example 4, then the object the script is attached to, gets teleportet to the position of the step 4 variable.
Answer by paul_hayes · Jun 14, 2014 at 01:24 PM
I'm guessing a bit here, as I'm not sure what you Dice.steps variable type is, I've assumed it's an integer that is the number of steps forward to move along the list.
Rather than using a new variable for each step, you can make an array of steps, and it will appear in the editor as a list, which you can just drag new objects onto, to add them to the list.
#pragma strict
var steps : Transform[];
var stepIndex : int;
function Start () {
}
function Update () {
var nextSteps = gameObject.Find("Board").GetComponent(Dice).steps;
if(Input.GetKey("t")) {
stepIndex = stepIndex + nextSteps;
if( stepIndex >= steps.Length ) stepIndex = steps.Length-1;
if( stepIndex > 0 ) {
transform.position = steps[stepIndex].position;
}
}
}
Answer by MrFijiWiji · Jun 14, 2014 at 01:30 PM
Well, to begin with, that's a lot of Transforms to be typed like that, which will only cause you to have to write a lot more code down the line for such a simple task. Instead consider creating an array to hold all of your steps, this will streamline everything and save you a lot of typing in the long run.
If you have a set number of steps, for example 14 in your script above and that number will never change, then you should consider using a built in array, while i'm not to familiar with UnityScript, i understand these to be the fastest option for such tasks. A built in array can be defined as such:
var steps : Transform[];
That will define an array of transforms that can be assigned in the inspector. Simply set your number of steps and drag and drop the transforms into the slots provided.
From there, you can do a number of things with the array. I'll leave the scripting documentation to provide you with all of the extras that can be used, but for now i'll show you how to move to a step that you have selected from the array.
To teleport an object to a step in the array, you could simply say:
if(Input.GetKey("t")) {
transform.position = steps[step].position;
}
Thank you. You also answered a other question i had with the array!
Your answer
Follow this Question
Related Questions
transform.position sends object to wrong position 1 Answer
transform position player teleport 0 Answers
Transform position? 2 Answers
Making The Hole In A Golf Game 1 Answer