Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NinjaRubberBand · Jun 14, 2014 at 01:09 PM · transformpositionteleport

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;     
         }
     }
 }
  
  
  
  

 
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

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.

Unity Scripting API: Array

To teleport an object to a step in the array, you could simply say:

 if(Input.GetKey("t")) {
 transform.position = steps[step].position;     
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NinjaRubberBand · Jun 14, 2014 at 01:43 PM 0
Share

Thank you. You also answered a other question i had with the array!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.position sends object to wrong position 1 Answer

transform position player teleport 0 Answers

Transform position? 2 Answers

I'm trying to teleport with a key press, but right now they only teleport for one second. How to fix?,With a key press, no OnTriggerEnter, how to teleport the player? 2 Answers

Making The Hole In A Golf Game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges