- Home /
Question by
Fork_Spoon_Knife · Jun 09, 2018 at 09:22 PM ·
c#animation3djumpanimationclip
3d Animated Jump Script
I have this jump script that has 3 different animation clips for jumping, midair, and landing. The goal is to be able to jump left and right to move the player gameObject a fixed number of units while in midair but not before. I simply want to move the player, while the animation gives the gameobject the illusion of leaving the ground plane, because the action is to be used as an evasive maneuver to avoid enemy projectiles, not to surpass obstacles.
MY CODE:
public class JumpScript : MonoBehaviour {
private float speed;
private float timer = 0f;
private int alpha = 0; // used to trigger gameobject movement actions
public AnimationClip jump; // animControl 3
public AnimationClip air; // animControl 4
public AnimationClip land; // animControl 5
private AnimationClip selector = null; // used to get the length of the animationClip as in selector.length
private Animator anim; // Idle animation is animControl 0
private Vector3 destination;
void Start () {
anim = gameObject.GetComponentInChildren<Animator> ();
}
void Update () {
float jumpDir = Input.GetAxis ("Vertical");
if (alpha == 2) transform.position = Vector3.MoveTowards (transform.position, destination, speed * Time.deltaTime); // controls the movement of gameobject
if (jumpDir > 0) {
destination = new Vector3 (transform.position.x, transform.position.y, transform.position.z + 1);
anim.SetInteger ("AnimControl", 3);
speed = 50f;
selector = jump;
timer = selector.length;
StartCoroutine (jumpAnim ());
}
if (jumpDir < 0) {
destination = new Vector3 (transform.position.x, transform.position.y, transform.position.z - 1);
anim.SetInteger ("AnimControl", 3);
speed = 50f;
selector = jump;
timer = selector.length;
StartCoroutine (jumpAnim ());
}
}
IEnumerator jumpAnim() {
yield return new WaitForSeconds (timer); //waits for animationclip to finish
selector = air;
timer = selector.length;
anim.SetInteger ("AnimControl", 4);
alpha = 2;
yield return new WaitForSeconds (timer);
selector = land;
timer = selector.length;
anim.SetInteger ("AnimControl", 5);
yield return new WaitForSeconds (timer);
anim.SetInteger ("AnimControl", 0);
alpha = 0;
selector = null;
yield return null;
}
}
The problem with my current script is that the animations are wonky, asynchronous with the movement, only sometimes play, and its code heavy. I have looked but I have not found a good way to solve my current problem on the internet.
Comment