Reputation: 51
I have an arm, it is working nice but, when I try to throw a object, it doesn't work.
After some thoughts I made this:
cube.GetComponent<Rigidbody2D>().velocity = player.GetComponent<Rigidbody2D>().velocity;
And this happened:
I tried to attach a rigidbody to the arm, but he falls with the gravity, and I tried to turn gravity off, but it doesn't work.
My question is, how do I make it work with the arm?
Edit: the entire code for better explanation(it is attached to the hand):
void Update () {
Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);
mao1.SetBool("Ligado", ligado);
if (Input.GetKey(KeyCode.LeftShift)) {
ligado = true;
} else {
ligado = false;
}
if (Input.GetKey(KeyCode.LeftShift) && touch != null && ligado == true)
{
touch.gameObject.transform.parent = this.transform;
cubo.GetComponent<Rigidbody2D>().isKinematic = true;
Physics2D.IgnoreLayerCollision(10, 12, true);
cubo.GetComponent<Rigidbody2D>().velocity = player.GetComponent<Rigidbody2D>().velocity;
}
else if (touch != null && ligado == false)
{
touch.gameObject.transform.parent = null;
cubo.GetComponent<Rigidbody2D>().isKinematic = false;
Physics2D.IgnoreLayerCollision(10, 12, false);
}
}
Upvotes: 1
Views: 536
Reputation: 322
If I understand well you press LeftShift when the cube is in contact with the tip of your arm and it "glue" the cube to the arm, then when you release LeftShift you want to throw the cube in a certain direction.
You can achieve that using the AddForce function in Rigidbody2D with a carefully computed vector :
// In the script outside of a function
public Transform greenCubeCenter;
// In your Update function
else if (touch != null && ligado == false)
{
touch.gameObject.transform.parent = null;
cubo.GetComponent<Rigidbody2D>().isKinematic = false;
Physics2D.IgnoreLayerCollision(10, 12, false);
// Below the important part
Vector2 force = (touchDetect.position - greenCubeCenter.position).normalized * ThrowForceMagnitude; // ThrowForceMagnitude needs to be defined somewhere
touch.GetComponent<Rigidbody2D>().AddForce(force);
}
And just put a transform which is the center of the cube in the greenCubeCenter variable.
This algorithm should (and again if I understood everything well) throw the object away from your cube in the direction Cube - Tip of the Arm with a force of ThrowForceMagnitude (that you need to define somewhere)
UPDATE :
Okay after understanding everything a bit further here is a more complete answer :
public Transform greenCubeCenter;
public float ThrowForceMagnitude = 50.0f;
Transform touchedObject = null; // this will hold the touched object
void Update () {
Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);
if(touch == null) // we don't do anything if we don't touch anything
return;
if (Input.GetKey(KeyCode.LeftShift)) { // let's do everything in one if
ligado = true; // if the arm touch the cube we set ligado to true
if(touchedObject == null) // we didn't have anything grabbed
{
touchedObject = touch.gameObject.transform;
touchedObject.parent = this.transform; // "glue" the object to the arm
touchedObject.GetComponent<Rigidbody2D>().isKinematic = true; // forbid other forces to move our object
Physics2D.IgnoreLayerCollision(10, 12, true); // I let this thing here, don't really know what it does in this context
}
} else {
ligado = false; // in any case we're not in "grab mode" anymore so let's set ligado to false
if(touchedObject != null) // if we were grabing something
{
touchedObject.parent = null; // let the object free
touchedObject.GetComponent<Rigidbody2D>().isKinematic = false; // let it be affected by reality again
Physics2D.IgnoreLayerCollision(10, 12, false); // restoring the think I didn't understand before hand
// Below the actual throwing part
Vector2 force = (touchDetect.position - greenCubeCenter.position).normalized * ThrowForceMagnitude;
touchedObject.GetComponent<Rigidbody2D>().AddForce(force); // actually throwing the object
touchedObject = null; // we let the object go so we set touchedObject to null
}
}
mao1.SetBool("Ligado", ligado); // updating display
}
It is not a perfect code, algorithmically speaking (is that even a word) it should be correct but you should dive into it line by line and understand everything (I think I commented enough).
Note that I removed the reference to Cubo, I think you could grab anything in the objectLayer.
Upvotes: 1