Unity|使用枚举状态控制角色移动

enum:

    //移动枚举
    public enum STEP
    {
        NONE = -1,
        RUN = 0,            //奔跑 游戏中
        STOP,               //停止 显示得分时
        MISS,               //失败 和怪物解除
        NUM,
    };

Update:

//各个状态的执行

        //位置
        switch (this.step)
        {
            case STEP.RUN:
                {
                    //速度
                    if (this.is_running)
                    {
                        this.run_speed += OwnPlayerControl.run_speed_add * Time.deltaTime;
                    }
                    else
                    {
                        //减速
                    }
                    this.run_speed = Mathf.Clamp(this.run_speed, 0.0f, RUN_SPEED_MAX);

                    //应用于本rigidbody的velocity
                    Vector3 new_velocity = this.GetComponent<Rigidbody>().velocity;
                    new_velocity.x = run_speed;

                    //y坐标速度不变
                    if (new_velocity.y > 0.0f)
                    {
                        new_velocity.y = 0.0f;
                    }

                    //应用new_velocity
                    this.GetComponent<Rigidbody>().velocity = new_velocity;//Vector3

                    //按W键快速前进 用于调试
                    if (Input.GetKeyDown(KeyCode.W))
                    {
                        Vector3 new_position = this.transform.position;
                        new_position.x += 100*OwnFloorControl.WIDTH * OwnFloorControl.MODEL_NUM;
                        this.transform.position = new_position;
                    }

                    //按space空格跳跃 用于调试
                    if (Input.GetKeyDown(KeyCode.Space))
                    {
                        Vector3 new_position = this.transform.position;
                        new_position.y += JUMP_HEIGHT;
                        this.transform.position = new_position;
           
                    }
                }
                break;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • title: "Swift 中枚举高级用法及实践"date: 2015-11-20tags: [APPVENTUR...
    guoshengboy阅读 2,623评论 0 2
  • 用了mysql好多年,很少关注mysql自带库,自然也不知道这个库里存放些什么,在看《MySQL排错指南》时看到它...
    灼灼2015阅读 1,915评论 1 3
  • 最近,从宝贵的时间里挤出了整整一个月,拿来到山里种地。最初我以为只是种地。没有想到收获会远远超出种地本身。 有一句...
    南山居阅读 880评论 0 0
  • 需求满足理论的应用:应用的研究领域很多,但没有展开讲解 满足的影响:感情、认知、性格特质、人际关系以及其他,通过对...
    于辉的简书阅读 153评论 0 0
  • 老房子在城市闹市与郊区交界处,外观上与电影中上世纪上海街道旁的房子很像,或许老房子就是那时候留下来的,也或许比那更...
    七猫安阅读 240评论 3 1