Contrary to the title I mean taking an object, such as a cube or fist model, and say when you hit mouse button 1 the fist or cube “punches.” I have been trying to figure this out for several hours and I know it should be pretty simple but I don’t know what to do. Help is much appreciated, thanks!
Here is a simple punching script. Note if you want the punch to interact with Rigidbodies, it would be best to rewrite the manipulation of the transform to use Rigidbody.MovePosition().
#pragma strict
var punching = false;
function Update() {
if (!punching && Input.GetKeyDown(KeyCode.Space)) {
Punch(0.5, 1.25, transform.forward);
}
}
function Punch(time : float, distance : float, direction : Vector3 ) {
punching = true;
var timer = 0.0;
var orgPos = transform.position;
direction.Normalize();
while (timer <= time) {
transform.position = orgPos + (Mathf.Sin(timer / time * Mathf.PI) + 1.0) * direction;
yield;
timer += Time.deltaTime;
}
transform.position = orgPos;
punching = false;
}