- Home /
Make melee attack in 2D game
Hello guys, I'm trying to add a melee attack to my 2D game.
I wrote this, because I think I should change the center of the mass of the sword because it should rotate as if the player hold it from the grip. The problem is that the centerOfMass set method is not implemented yet, so I don't know how to make this. Any hint?
public class StuffController : MonoBehaviour {
public KeyCode action;
bool hit=false;
float goBack = 0;
float rate = 0.2f;
void Start(){
Debug.Log(""+rigidbody2D.centerOfMass);
rigidbody2D.centerOfMass = new Vector2 (0.6f, 0.5f);
Debug.Log(""+rigidbody2D.centerOfMass);
}
void Update () {
if (Input.GetKeyDown (action) && !hit) {
rigidbody2D.MoveRotation (-60);
goBack = Time.time + rate;
hit= true;
}
else if (hit && Time.time > goBack) {
hit=false;
rigidbody2D.MoveRotation(60);
}
}
}
$$anonymous$$ake it $$anonymous$$inematic and disable gravity on it.
I've not played around with 2D rigidbodies but in 3D, when an object is in your players hand it moves with it, if the object is parented to the Hand bone. Of course, this applies to Rigged objects.
Answer by Vitor_r · Aug 04, 2014 at 03:40 PM
You can try set it directly http://docs.unity3d.com/ScriptReference/Rigidbody2D-centerOfMass.html