Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 VisLock · Oct 16, 2015 at 11:44 AM · teleporting

Teleporting 2 objects

I am trying to do this part in a code were 2 objects switch places with each other but I am not sure how to do it. Can you please help ?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xarbrough · Oct 16, 2015 at 11:47 AM

 public Transform myObjectA;
 public Transform myObjectB;
 
 private void Start()
 {
     Vector3 tempPosition = myObjectA.position;
     myObjectA.position = myObjectB.position;
     myObjectB.position = tempPosition;
 }


That's basic swapping, but if you're starting out, you should watch some of the tutorial on the Unity learn site. This one seems to incorporate some of the needed aspects and maybe gives you some inspiration: Teleportation Grenades

Comment
Add comment · Show 8 · 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 VisLock · Oct 16, 2015 at 11:51 AM 0
Share

thank's I am going to try and make it work.

avatar image VisLock · Oct 16, 2015 at 11:55 AM 0
Share

how come you need a extra position (tempPosition) ?

avatar image Xarbrough · Oct 16, 2015 at 02:51 PM 1
Share

Better question: How would you do it without a tempVector? If you change one of the transforms, you lose that information unless you previously store it in a temporary variable.

avatar image Bunny83 Xarbrough · Oct 16, 2015 at 03:11 PM 0
Share

Yep, you usually need a temp variable. However for simple datatypes (int, byte, ...) you can use "xor" to swap two values without a temp variable:

 // C#
 int a = 5; // 0101
 int b = 3; // 0011

 a = a ^ b; //                                          a = 0101^0011 = 0110
 b = a ^ b; // b = a ^ b ^ b --> b = a                  b = 0110^0011 = 0101 (former a)
 a = a ^ b; // a = a ^ b ^ a (the new b) --> a = b      a = 0110^0101 = 0011 (former b)

It's an old trick the usually isn't worth the hassle.

That's why i love LUA. In lua there are multi-values. A swap in lua looks like this:

 // LUA
 a,b = b,a

avatar image VisLock Bunny83 · Oct 17, 2015 at 04:35 PM 0
Share

Cool that's very interesting

avatar image VisLock Xarbrough · Oct 17, 2015 at 04:35 PM 0
Share

Your code worked thank you so much :)

avatar image VisLock Xarbrough · Oct 17, 2015 at 04:44 PM 0
Share

my last question is why is it a vector3 im not really sure how or when i need a vector3 ?

avatar image Xarbrough VisLock · Oct 17, 2015 at 04:50 PM 0
Share

Vector $$anonymous$$aths Unity Tutorial

Understanding Vector Arithmetic

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I enter/exit structures? 1 Answer

Switch from from Exterior scene to Interior Scene? 1 Answer

FPSContoller Teleportation Script is blocking somehow 2 Answers

Grappling Hook Script 0 Answers

I need help my teleporting script?Can anybody help me? 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