Scroll Game_Character Aninmation

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");
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,129评论 0 23
  • 章肆 九:权柄 帝辛今天起的特别早,他戴上面具,屏退宫人,拿上自己的小斧头去御花园砍了一会儿竹子,待额头出了一层薄...
    林喜喜阅读 291评论 2 3
  • 记录文字就是记录生命的成长!不管以后如何,每天八百字的文字生活开始罗!写没得写的,写有的写的,写感想,写育儿,写...
    跳舞的西服阅读 284评论 0 0
  • 早上用水壶打水喝,发现饮水机上没有水了,自言自语地说了声“没有水了”,很轻,准备等有水了再来装。没想到本来走到门口...
    青云墨阅读 248评论 0 0