- Home /
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
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.
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.
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!
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