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 Helgeman · Apr 12, 2011 at 01:10 PM · objectsswapping

Swapping Objects

Hi

I am a COMPLETE CODER NOOB so be gentle.

Basically i have for now created a prefab. Of which at the beginning of the game 150 are created =) So what i want to do as a next step is swapping them.

This should work the following way: I click on object (Cube) and then it somehow saves the position of it and then when i click on the next one they swap positions. This would be best with if they would slowly move into thei new place instead of just being there but that would be the minor problem.

I am really lost what to do now. I looked through this forum and the documentation but didnt really find anything that suits my problem

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jesse Anders · Apr 12, 2011 at 09:33 PM

Same advice as I always give: break it down into separate problems, and solve the problems one by one.

First, you need to be able to determine when an object has been clicked on. There are multiple ways to do that, but a fairly straightforward way is to give the object a collider and use OnMouseDown(). Start by writing a script that just prints "I was clicked!" or whatever to the console when an object is clicked on, just to make sure that part's working.

Then, you'll probably need a simple state machine. The FSM will only have two states, 'waiting for the first click', and 'waiting for the second click'.

When you first click on a box, you save a reference to the box that was clicked on, and change the state to 'waiting for the second click'. Then, on the next click, you swap the 'saved' box with the box that was just clicked. An example in pseudocode:

if mouse button was clicked
    if state == waiting for first click
        saved box = clicked box
        state = waiting for second click
    else if state == waiting for second click
        swap(saved_box, clicked box)
        state = waiting for first click
    end if
end if

You'll need to implement swap() of course. As for animating the effect, you could have a third state ('swapping boxes') or use a coroutine, and animate the effect using, for example, Vector3.Lerp() and an 'ease-in, ease-out' mapping function.

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

Answer by Aleks 1 · Apr 12, 2011 at 09:35 PM

movement-wise, I would use itween (just google)

make sure to get the visual editor also

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

Answer by Maulik2208 · Dec 28, 2012 at 09:28 AM

 void OnMouseUp()
     {
         Vector3 temp = GameObject1.transform.position;
         GameObject1.transform.position = GameObject2.transform.position;
         GameObject2.transform.position = temp; 
     }

This will solve your Problem......If found useful then don't forget to mark the answer.....Cheers.....Enjoy......

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Swaping to a camera attached to another object 2 Answers

Falling object passes through ground objects - Need help 1 Answer

Platform should bend when object falls 0 Answers

Counting active objects in a list 3 Answers

BoxColliders go off when object SetActive 0 Answers


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