I am trying to build a boxer who is always looking at the player, and punches at the player when in range. The punching is being done with AddForce
, with the glove’s forward as the direction. No matter what direction the boxer and glove face the glove moves in the same direction (0, 0, -1);
This is the code I am using, it’s on the boxer which is a parent of the glove.
gloveRB = glove.GetComponent.<Rigidbody>();
function Update() {
transform.LookAt(target.transform);
var HitInfo : RaycastHit;
var gloveDirection = glove.transform.InverseTransformDirection(gloveRB.velocity);
if (gloveDirection.magnitude < 0.1) {
if (Physics.Raycast(glove.transform.position, glove.transform.forward, HitInfo, 2)) {
if(HitInfo.transform.gameObject.tag == 'boxer') {
Debug.DrawRay(glove.transform.position, glove.transform.forward * 2, Color.blue, 20);
gloveRB.AddForce(glove.transform.forward * 10 * Time.deltaTime, ForceMode.Impulse);
}
}
}
It maybe worth noting that I have a spring joint connecting the glove and boxer so that the glove is drawn back after the punch.
I am using ForceMode.Impulse
because I only want this force added all at once.
The debug ray is drawn in the correct direction, and the glove should follow it but it just moves in (0, 0, -1)
Why isn’t the force being added in the right direction?