I trying to pick a ball from one cup and drop it in an other cup with a tong sort of thing. New to unity and script. Pls help with the script or how to accomplish this in unity.
This isn't a trivial topic, but the easiest way would probably be to add a trigger game object to the end of your tongs, then parent the tongs to the ball temporarily. I haven't had time to test any of the following code so I'm sure there are syntax errors, but perhaps this will serve as a starting point for this discussion. By the way, you'll need to drop your tong object into the variable in the inspector.
You could also do it with a hinge or fixed joint if your two objects are rigidbodies. That can be scripted as well.
private var grab : boolean = false;
var tong : Transform;
private var grabObject: Transform;
function Update()
{
if (Input.GetButtonDown("Fire1") && grab)
{
grabObject.parent = tong;
}
else if (Input.GetButtonDown ("Fire1") && !grab && grabObject)
{
grabObject.parent = null;
}
}
function OnTriggerEnter (hit : Collider)
{
grab = true;
if (hit.gameObject.name == "ball";
{
grabObject = hit.transform;
}
}
function OnTriggerExit ()
{
grab = false;
}