美文网首页
Scroll Game_Character Aninmation

Scroll Game_Character Aninmation

作者: DarkKnightRedoc | 来源:发表于2017-09-18 15:39 被阅读0次

The script and animator achieve an animation effect like this:

Animation.gif Animator.png
// Created by John Ma,
// ©All rights reserved. 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnityChanAnimation : MonoBehaviour {
    private Animator UnityChanAC; // unitychan's animation controller


    // successional key pressed detection viriable
    float FirstPressTime = -100; // time for first press key
    float SecondPressTime = 100; // time for second press key
    private bool FirstPress = false; // bool variable used for successional key pressed detection

    // Use this for initialization
    void Start () {
        UnityChanAC = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update () {
        WalkAndRun();
        Jump();
        Turn();
    }

    void Turn()
    {
        if ( (FacingRight() && Input.GetKeyDown(KeyCode.LeftArrow)) 
            || (FacingLeft() && Input.GetKeyDown(KeyCode.RightArrow)
            && isIdleing()) )
        {
            UnityChanAC.SetTrigger("Turn");
        }
    }

    bool FacingRight()
    {
        return Mathf.Abs(this.gameObject.transform.localEulerAngles.y - 90) < 0.5;
    }

    bool FacingLeft()
    {
        return Mathf.Abs(this.gameObject.transform.localEulerAngles.y - 270) < 0.5;
    }

    void Jump()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow) && IsRunning() == true) // when running and key is pressed, jump
        {
            UnityChanAC.SetTrigger("Jump");
        }
    }

    void WalkAndRun()
    {
        if (Input.GetKeyDown(KeyCode.RightArrow) && FacingRight()) // walk and run while facing right
        {
            bool onechance = false; // only one chance for each press, in case it records both first and second press

            StartWalking(); // start walking on key down

            if (FirstPress == false) // 
            {
                FirstPressTime = Time.time;
                FirstPress = true;
                onechance = true;

                //Debug.Log("FP" + FirstPressTime);
            }

            if (FirstPress == true && onechance == false) { 
                SecondPressTime = Time.time;
                FirstPress = false;

                //Debug.Log("SP" + SecondPressTime);
            }
            if (Mathf.Abs(SecondPressTime - FirstPressTime) < 0.5f && FacingRight()) // succession pressing, run
                StartRunning();
            else
            { }
        } else if (Input.GetKeyDown(KeyCode.LeftArrow) && FacingLeft()) {// walk and run while facing left
            bool onechance = false; // only one chance for each press, in case it records both first and second press

            StartWalking(); // start walking on key down

            if (FirstPress == false) // 
            {
                FirstPressTime = Time.time;
                FirstPress = true;
                onechance = true;

                //Debug.Log("FP" + FirstPressTime);
            }

            if (FirstPress == true && onechance == false)
            {
                SecondPressTime = Time.time;
                FirstPress = false;

                //Debug.Log("SP" + SecondPressTime);
            }
            if (Mathf.Abs(SecondPressTime - FirstPressTime) < 0.5f && FacingLeft()) // succession pressing, run
                StartRunning();
            else
            { }
        }

        if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow))
        {
            StopWalking();
            StopRunning();
        }

    }

    void StartRunning()
    {
        UnityChanAC.SetBool("Running", true);
        UnityChanAC.SetBool("Ideling", false);
    }

    void StopRunning()
    {
        UnityChanAC.SetBool("Running", false);
    }
    void StartWalking()
    {
        UnityChanAC.SetBool("Walking", true);
        UnityChanAC.SetBool("Ideling", false);
    }

    void StopWalking()
    {
        UnityChanAC.SetBool("Walking", false);
        UnityChanAC.SetBool("Ideling", true );
    }

    bool isIdleing()
    {
        return UnityChanAC.GetBool("Ideling");
    }

    bool IsRunning()
    {
        return UnityChanAC.GetBool("Running");
    }
}

相关文章

网友评论

      本文标题:Scroll Game_Character Aninmation

      本文链接:https://www.haomeiwen.com/subject/eodbsxtx.html