• 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 getgcs · Jan 16, 2013 at 03:51 AM · objectmovemove to

How to move a cube to another cube

How to move a cube to another cube when I click the cube.

When I click cube1 I need cube1 to find the position of cube2

Then I need cube1 to go to cube2.

I looked for answers on this but none were easy for me to understand sorry.

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
1
Best Answer

Answer by robertbu · Jan 16, 2013 at 05:50 AM

Here is a script that does not involve Raycasts. It moves the object immediately (not over time). If both objects are the same size and shape, then the moved object will seem to disappear (since they are at the same position).

 using UnityEngine;
 using System.Collections;
 
 public class MoveObjectTo : MonoBehaviour {
     public Transform transDest;  // Destination 
     void OnMouseDown()    {
         transform.position = transDest.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
1

Answer by Sooper1337 · Jan 16, 2013 at 04:02 AM

You can set up a Raycast to detect when the mouse clicks on the cube, then set the transform.position of cube 1 to cube 2.

     public Transform cube1;
     public Transform cube2;
     
     void Update () {
         RaycastHit hit;
         
         if(Input.GetMouseButtonDown(0)){
             Ray _Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast(_Ray, out hit, 100f)){
                 GameObject hitObj = hit.collider.gameObject;
                 if(hitObj.tag == "cube 1"){
                     cube1.transform.position = cube2.transform.position;
                 }
             }
         }
     }

Comment
Add comment · Show 9 · 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 getgcs · Jan 16, 2013 at 04:08 AM 0
Share

Im not asking what to do.. I am asking how to do it.. that is like saying I can do it.. I need an example or a specific reference link.. thanks though

avatar image Sooper1337 · Jan 16, 2013 at 04:14 AM 0
Share

Havnt Had A Chance to test that but it should work. also you will need to set the tags for the cube in order for it to work.

avatar image Sooper1337 · Jan 16, 2013 at 04:22 AM 0
Share

Updated Code works just set each cube to the appropriate transform in the inspector, and set the tag for cube 1 as "cube 1", and it should work when you click cube 1.

avatar image getgcs · Jan 16, 2013 at 04:22 AM 0
Share

wow to move a cube to a cube you need a paragraph of code.. man I thought this engine was easy.. It can not be easy if it relies this much on code. So I guess the engine could have asked me what I wanted to do with the cube when I created it and then gave me more questions to narrow down the tutorials I could then pick from. Hmmm now that would be easier right?

How can I do this without ray stuff? Is it possible to do this in a different way?

Thanks very much for your effort and quick response. I wish for all of us that it was easier and I am sure in time we will get there. Lastly, I have picked this engine for a reason and eventually they will give me more and more reasons to stay with it.

avatar image Sooper1337 · Jan 16, 2013 at 04:25 AM 0
Share

As for my knowledge to actually have it where you click on the cube itself and have some interaction you have to have a ray-cast so unity knows that you are clicking it. if you just want to move the cube when you press $$anonymous$$ouse 1 then you do not have to have ray-casting, although that wasn't what you asked for. If you are just starting out don't get discouraged, over time you will learn it and it will be a lot easier.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

How should I move an object that parents on collision? 1 Answer

using Transform on a Object will not move it 100% exactly... 1 Answer

Rotate object with another object 0 Answers

touch move slide object 0 Answers

Move another cube with a cube 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges