I'm very new to game development, I mostly program other things. I'm trying to write a basic whack-a-mole type of game. I've gone ahead and created a hammer with a swinging animatio in the FBX. I've also attatched a box collider to the head of the hammer.
The issue is that when I play the hammer swing animation (and the hammer moves down), the box collider does not follow. I understand that this is because the animation is only for the object, but in this case it presents a problem since I need the collider to be at the hammer's lowest point once the animation gets there.
What is the right way to approach this problem? I've thought of a couple solutions on my own, are any of these valid?
1) Move the Collider with the animation. I've got no clue on how to do this.
2) Store the time when I start the animation, check if enough time has ellapsed for completed animation. Then manually move collider to the contact point. Wait a few milliseconds, then move it back. I tried this, the issue with this is that it moves the entire object with the collider.
void Update() {
if (Input.GetMouseButtonDown(0)) {
animator.SetTrigger("hammer");
animationStartTime = Time.time;
toAnimate = true;
}
if(Time.time-animationStartTime > 0.38 && toAnimate == true) {
toAnimate = false;
//bc = attatched box collider
bc.transform.position = new Vector3(0, 0, 0); //move to target point (won't be 0,0,0 for real, just a test point)
}
if(Time.time- animationStartTime > 0.50)
{
bc.transform.position = transform.position + bcOffset; //reset to original position
}
}
3) Do what I did in step 2, but create a collider not attatched to a parent object so that the parent object isn't moved. I don't know how to create a collider on its own however.
BoxCollider bc = new BoxCollider();
//I then use bc.size and bc.position and set them but it does not show up
//on my scene view so I don't think this works.
NOTES:
This script is attatched to hammer object.
The hammer is moved with this code:
hPos = Input.GetAxis("Mouse X") * -0.7f;
fPos = Input.GetAxis("Mouse Y") * 0.7f;
transform.position += new Vector3(fPos, transform.position.y, hPos);
Edit: is it possible for me to apply the animation to the collider? This would probably be the easiest way.