Ok I have been struggling for days now trying to figure out how to code in javascript to get a mechanical arm in my game to pick up a rigidbody and then drop it whenever you touch a guitexture button on the iPad screen. I have searched through the forum and UnityAnswers and the web, but I can’t seem to find an example for this. Everything that comes up in my search results is geared towards drag and drop with a mouse click rather then touch a GUITexture button to parent a rigidbody and touch again to unparent.
My goal is to use the same GUITexture button on screen… touch the button once and the mechanical arm animates down, picks up the rigidbody and then animates back up. Touch the same GUITexture button again and then the mechanical arm animates down again but this time drops the rigidbody. I would also like the mechanical arm to be invisible before you touch the GUITexture button so it “magically” appears and animates down after you touch the button. So far I have been able to pick up a rigidbody, but I can’t seem to drop it. I also only want to be “holding” one rigidbody object at a time. The objects in my scene that I want to pickup I have given them a tag called “pickup”. I have attached my script and a sphere collider object to a child of my mechanical arm game object (ie. to the “hand” of the mechanical arm)… I have pasted my script below. Please tell me if there is something I’m overlooking.
var mechanicalArm: GameObject;
var LaunchMechArmButton : GUITexture;
var itemsHolding: int;
var buttonPressCount: int;
var mechArmVisible: boolean;
// make the mechanical arm invisible by deactivating it's game object along with all of it's children
//mechanicalArm.SetActiveRecursively(false);
function Update ()
{
// begin "for" loop to check for iPhone touches and create a variable "evt" to store them in
for (var evt : Touch in Input.touches)
{
// hit tests: create variables to store hit test results then test for hit inside each GUITexture
var LaunchMechArmButtonHitTest = LaunchMechArmButton.HitTest(Input.mousePosition);
// first check to for which touch phase (Began, Stationary, Ended)
if (evt.phase ==TouchPhase.Began)
{
// nested if statements that tell what to do if each hit test is true
if(LaunchMechArmButtonHitTest)
{
// make mechanical arm visible by activating it's game object and all it's children
//mechanicalArm.SetActiveRecursively(true);
// play sound effect for mechanical arm
audio.Play();
// store a boolean value for whether or not the mechanical arm is visible
mechArmVisible = true;
// play the animation of the mechanical arm when button is touched
mechanicalArm.animation.Play("Take 001");
// keep track of how many times this button has been pressed or touched
buttonPressCount++;
}
}
}
}
function OnTriggerEnter (other : Collider)
{
// Get the transform of the pickup
var collisionTransform = other.gameObject.transform;
//Only pick up a game object with a tag of "pickup"
if (other.gameObject.tag == "pickup")
{
if(itemsHolding < 1)
// make it a child of the current object
collisionTransform.parent = transform;
// Don't let the rigidbody be affected by physics!
collisionTransform.rigidbody.isKinematic = true;
// Turn Gravity OFF so rigidbody doesn't fall through the floor
collisionTransform.rigidbody.useGravity = false;
// make it point towards the object that picked it up
// place it behind the current object
//collisionTransform.localPosition = -Vector3.forward * 5;
collisionTransform.LookAt(transform);
// Keep track of how many items you are holding
itemsHolding++;
}
}
// item management
function OnTriggerStay (other : Collider)
{
var collisionTransform = other.gameObject.transform;
if (itemsHolding > 0 && buttonPressCount > 1 && mechArmVisible)
{
collisionTransform.parent = null;
itemsHolding--;
audio.Play();
mechanicalArm.animation.Play("Take 001");
yield WaitForSeconds (2);
collisionTransform.parent = null;
collisionTransform.rigidbody.isKinematic = false;
collisionTransform.rigidbody.useGravity = true;
collisionTransform = null;
}
}