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 getgcs · Jan 05, 2013 at 01:33 AM · objectclickclick and drag

How do I click an object and have it go to another object

How do I click a ball for example and have it go to another object like my hand

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

Answer by save · Jan 05, 2013 at 01:45 AM

Detecting clicks on objects can be done by using the OnMouseDown function or Raycasting.

Moving objects towards another can be done by translating towards direction of target (target position - object position), lerping or using other move algorithms inside the Vector3 struct or the Mathf collection.

Edit: Regarding your previous statement,

holy crap.. so I thought learning this would be step one.. tell me an example of on mouse down target position - object position.. that might help

It might sound overwhelming at first if you're not used to scripting/programming. Unity has plugins for more visual scripting if you rather get familiar this way with making games. Scripting-wise there's actually a lot of help in the documentation's script samples, descriptions and their derived behaviors. Having a look at the OnMouseDown function tells us that this function only works for objects with colliders and GUI-elements and it doesn't work for iPhone. Hence the Raycasting solution which is more controllable. Moving an object from a position towards another is the next step, doing this needs some sort of algorithm that moves an object towards another over time, luckily Unity provides us with a couple of them. You can use this script at the bottom of the page to try them out and see how they work. There you also have movement code (for several positions in a row).

There's no exact correct way of doing this as it differs from the needs of the game, but to make the magic happen - here's a general example of how to select and move objects:

SelectAndMoveObject.js

 #pragma strict
 
 var layerMask : LayerMask; //What types of objects should be able to move?
 var targetTransform : Transform; //Where do we move the objects?
 var parentToTarget : boolean = true; //Do we parent the object to target?
 var distance : float = 10.0; //How far away can we select objects?
 var timeToMove : float = 2.0; //How long does it take to move the object?
 
 private var moveTransform : Transform; //The selected transform that we currently move
 private var cam : Camera; //The camera we use to shoot raycasts
 
 function Start () {
     cam = Camera.main; //Assigns the main camera
 }
 
 function Update () {
     //If we press left mouse button try to grab that object
     if (Input.GetButtonDown("Fire1")) GrabObject();
 }
 
 function GrabObject () {
     //Construct a ray from mouse position in camera
     var ray : Ray = cam.ScreenPointToRay(Input.mousePosition);
     
     //Construct the variable that holds the raycast out-info
     var hit : RaycastHit;
     
     //Shoot that ray out into the scene, if it hits something the value returns true with out-info
     if (Physics.Raycast(ray, hit, distance, layerMask)) {
         if (moveTransform!=null) ReleaseObject(moveTransform);
         moveTransform=hit.transform;
         MoveObject(moveTransform);
     }
 }
 
 function MoveObject (t : Transform) {
     //Moves object towards position while conditions are met, first see if the object uses physics
     var rb : Rigidbody = t.GetComponent(Rigidbody);
     if (rb) rb.isKinematic = true;
     if (parentToTarget) t.parent = targetTransform;
     var timeStamp : float = Time.time;
     var posStamp : Vector3 = t.position;
     while (t==moveTransform && Time.time<timeStamp+timeToMove) {
         t.position =  Vector3.Lerp(posStamp, targetTransform.position, (Time.time - timeStamp)/timeToMove);
         yield;
     }
 }
 
 function ReleaseObject (t : Transform) {
     //Releases object when new object is selected
     var rb : Rigidbody = t.GetComponent(Rigidbody);
     if (rb) rb.isKinematic = false;
     if (parentToTarget) t.parent = null;
 }

I tried to explain most of it in comments but of course feel free to ask questions.

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 youngapprentice · Jan 05, 2013 at 01:53 AM

iTween is another valuable asset for you. You can cache the point it needs to go to at the start. Also, if you'd like to keep the object in the hand, you might want to use transform.parent.

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 getgcs · Jan 05, 2013 at 06:49 AM

holy crap.. so I thought learning this would be step one.. tell me an example of on mouse down target position - object position.. that might help

Thanks!

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 AlucardJay · Jan 05, 2013 at 06:49 AM 0
Share

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

Under the answer where it says edit | delete | more , click on more , then convert to comment

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

11 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

Related Questions

Trying to make Quizlike game. Click on object, get right or wrong response 0 Answers

Some Copies of Prefab Detect Clicks But Others Don't 1 Answer

Object Movement via Mouse Click? 3 Answers

How to make 3D object become the cursor? 2 Answers

Click and drag an object 3 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