- Home /
Unity 2D Weapon Pick Up And Striking/Swinging
I think my question is pretty straightforward: You have the player and some objects containing my own script "Throwable". In my case beer bottles.
I already got the player to pick up those bottles and throw them. The center of the "InteractionPoint"(which is the white circle with the crossing diameters) is the position, where the bottles will be "held" once picked up. My script looks like this:
public bool PickUp(GameObject attachedTo)
{
gameObject.transform.parent = attachedTo.transform; // Add to parent
gameObject.transform.localPosition = attachedTo.transform.Find("InteractionPoint").localPosition; // Set position
m_collisionpoints.enabled = false; // no collision
m_Rigidbody2D.isKinematic = true; // no gravity
m_Rigidbody2D.velocity = new Vector2(0f, 0f); // no velocity
m_Rigidbody2D.angularVelocity = 0f; // no rotation
PickedUp(true, attachedTo); // picked up and parent
return true;
}
That works and looks like this: Now throwing isn't too bad either, just revert those manipulations done on the RigidBody and add a force to it, which also works fine. But I also want the player to be able to strike with the bottle. Maybe hold it upside down and attack with it. And this is where my problem lies: I have no idea how to tackle that idea.
Suppose we have a 2d animated frame-by-frame sprite and the interaction point is basically his "hand". I don't know where to start. Should it be an animation? Should I just script the InteractionPoint moving somewhere? How can I "swing" the bottle? As is it looks rather static, being fixed to the interaction point.
OK, I'm making a little bit of progress here, but not really. Once my character picks up a bottle, it becomes his child object. I can animate that child object, but my problem is, that I want this to be as general as possible. I don't know, what kind of object the player will hold and my animation clip needs to be applicable to other kind of child objects as well.
Answer by Tom4nt · Apr 03, 2021 at 11:02 AM
If I understand your situation correctly, I think the following solution would work for you:
Try to add an intermediate object (a pivot) to the InteractionPoint object. Then, you can animate the position, rotation and scale (if needed) of the pivot and it will animate relative to it's parent (InteractionPoint).
After that, at runtime, you can just parent the bottle or any other throwable to the pivot object and it will move/rotate/scale with it.
Let me know if I misunderstood your problem.
Sorry for any English mistake.