- Home /
take or pick object
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.
Answer by tool55 · Feb 08, 2011 at 03:49 AM
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;
}
thanks, I tried the script on a cylinder named 'tong' with a box collider named 'grab' at the end to grab a sphere named 'ball' . These are the errors I get. line 3 expecting func, found 'grabObject' ln 20 expecting ), found '='. ln 20 unexpected token: ball. ln 22 expecting: found'='. I tried to correct the syntax but failed
I'll work on it and re-post. I did this on the fly. I'll test it as soon as I can catch a few $$anonymous$$utes. Sorry for the errors.
Oh, and I left out var for the grabObject variable. Whoops. I edited the above to add it in. I'll check the other errors later
Your answer
Follow this Question
Related Questions
iphone roll a ball game 2 Answers
Get a free moving ball. 1 Answer
I WANT TO MAKE MY BALL JUMP :( 2 Answers
The Script keeps executing..instead executing onec 0 Answers
Best way to create a ball controlling system for a football game 0 Answers