【问题标题】:Character movement, changing players animation角色移动,改变玩家动画
【发布时间】:2016-11-09 04:43:25
【问题描述】:

我制作了一个用于移动角色和设置动画的脚本。当玩家想要向右走时,动画设置为角色面向右侧并行走的动画。一切都运行良好,直到您想同时向两个方向移动,比如说向上和向左。角色是这两种力量的合力,但动画正在变得疯狂。我为此做了快速修复,不允许在播放任何其他动画时左右播放。 我在想这样的事情: 当你向右走时,你面向右侧。继续前进,然后按下向上按钮,您将进入东北侧,但您仍然面向右侧。 所以你即将面对你想先去的那一面,在这个例子中是对的。

using UnityEngine;
using System.Collections;

public class player_movement : MonoBehaviour {
    //przyciski do poruszania się
    private KeyCode right_key = KeyCode.D;
    private KeyCode left_key = KeyCode.A;
    private KeyCode up_key = KeyCode.W;
    private KeyCode down_key = KeyCode.S;

    //poruszanie się gracza right == true => porusza się w prawo, left == true => porusza się w lewo; itp..
    private bool right;
    private bool left;
    private bool up;
    private bool down;

    player_statistics player; //zmienna przechowująca klasę? odnośnik do klasy? (yy nie wiem jak to nazwać)

    void Start ()
    {
        //przypisanie klasy do zmiennej, funkcji, klasy, no właśnie
        player = GameObject.FindWithTag("Player").GetComponent<player_statistics>();
    }

    void Update ()
    {
        movement_xy(); //wowołanie fukncji odp za wykrycie wciśnięcia klawiszy odp za poruszanie się
        movement_animation();
    }

    void FixedUpdate()
    {
        if (right || left || up || down) //wywołanie funkcji odp poruszanie się
        {
            movement_force();
        }
    }



    void movement_xy()
    {


        //ustawienie zmiennej odpowiedzialnej za ruch w prawo
        if (Input.GetKeyDown(right_key))
        {
            right = true;
        }
        if (Input.GetKeyUp(right_key))
        {
            right = false;
            GetComponent<Animator>().Play("idle_right", 0);
        }

        //ustawienie zmiennej odpowiedzialnej za ruch w lewo
        if (Input.GetKeyDown(left_key))
        {
            left = true;
        }
        if (Input.GetKeyUp(left_key))
        {
            left = false;
        }

        //ustawienie zmiennej odpowiedzialnej za ruch w górę
        if (Input.GetKeyDown(up_key))
        {
            up = true;
        }
        if (Input.GetKeyUp(up_key))
        {
            up = false;
            GetComponent<Animator>().Play("idle_up", 0);
        }

        //ustawienie zmiennej odpowiedzialnej za ruch w dół 
        if (Input.GetKeyDown(down_key))
        {
            down = true;
        }
        if (Input.GetKeyUp(down_key))
        {
            down = false;
            GetComponent<Animator>().Play("idle_down", 0);
        }
    }

    void movement_force()
    {
        // przykładanie do postaci danej sił fizycznej na osi X i Y
        if (left && player.player_gameobject.GetComponent<Rigidbody2D>().velocity.x > -5)
        {
            player.player_rigidbody.AddForce(new Vector2(-player.movement_speed, 0), ForceMode2D.Impulse);
        }
        if (right && player.player_gameobject.GetComponent<Rigidbody2D>().velocity.x < 5)
        {
            player.player_rigidbody.AddForce(new Vector2(player.movement_speed, 0), ForceMode2D.Impulse);
        }
        if (up && player.player_gameobject.GetComponent<Rigidbody2D>().velocity.y < 5)
        {
            player.player_rigidbody.AddForce(new Vector2(0, player.movement_speed), ForceMode2D.Impulse);
        }
        if (down && player.player_gameobject.GetComponent<Rigidbody2D>().velocity.y > -5)
        {
            player.player_rigidbody.AddForce(new Vector2(0, -player.movement_speed), ForceMode2D.Impulse);
        }
    }




    void movement_animation()
    {
        if(right && !up && !down) //quick fix, not like this helped but character animation isnt in crazy angry kid state
        {
            GetComponent<Animator>().Play("go_right", 0);
        }
        if (left && !up && !down)
        {
            GetComponent<Animator>().Play("go_left", 0);
        }
        if (up)
        {
            GetComponent<Animator>().Play("go_up", 0);
        }
        if (down)
        {
            GetComponent<Animator>().Play("go_down", 0);
        }
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    好的,我知道了。

        void movement_animation()
    {
        if(right && !left && !up && !down)
        {
            GetComponent<Animator>().Play("go_right", 0);
        }
        if (left && !right && !up && !down)
        {
            GetComponent<Animator>().Play("go_left", 0);
        }
        if (up && !down && !left && !right)
        {
            GetComponent<Animator>().Play("go_up", 0);
        }
        if (down && !up && !left && !right)
        {
            GetComponent<Animator>().Play("go_down", 0);
        }
    }
    

    void movement_animation()
    {
        if(right && !left && !up && !down) //quick fix, not like this helped but character animation isnt in crazy angry kid state
        {
            GetComponent<Animator>().Play("go_right", 0);
        }
        if (left && !right && !up && !down)
        {
            GetComponent<Animator>().Play("go_left", 0);
        }
        if (up && !down && !left && !right)
        {
            GetComponent<Animator>().Play("go_up", 0);
        }
        if (down && !up && !left && !right)
        {
            GetComponent<Animator>().Play("go_down", 0);
        }
    }
    

    没有更多的窃听动画。

    【讨论】:

    • 您可以通过使用 else if 并在其先前的 if 语句中删除随后测试的值来简化它。
    猜你喜欢
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2020-09-14
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多