zoukankan      html  css  js  c++  java
  • unity笔记-角色跳跃实现(2D)

    实现跳跃的3种方法:

    1、使用rigibody2d组件实现模拟重力,调用addforce()方法或直接给物体一个竖直方向的速度。

    代码如下:(与跳跃无关部分已省略)

    firstdemo:

    void Update()
        {
            if (Input.GetButtonDown("Jump"))
            {
                jumpKey = true;
            }
        }
    void FixedUpdate()
        {
           if (jumpKey && rg.IsTouchingLayers(ground)) //rigibody2d组件方法,检测物体是否接触某个layer,layer在inspector中设置
            {
                nextState = stateEnum.jumping;
                rg.velocity = new Vector2(rg.velocity.x, jumpForce);
                SwitchStateTo(stateEnum.jumping);
                AudioManager.instance.JumpAudio();
            }
            jumpKey = false;
            if (currentState == stateEnum.jumping && rg.velocity.y > 0.01f && !rg.IsTouchingLayers(ground))
            {
                SwitchStateTo(stateEnum.jumping);
            }
        } 

    跳跃物理效果放在fixedUpdate(),判定放在Update()中。

    2.使用rigibody2d,但其类型设置为kinematic(刚体运动学),地面设置为trigger,使用OnTriggerEnter2D()检测碰撞事件

    代码如下:

    notagame:

      void Update{
        #region
         if (Input.GetButtonDown("Jump")&& CurrentState!=Hero_states.jump)
            {
                CurrentState = Hero_states.jump;
                amControler.SetBool("run", false);
                amControler.SetBool("stand", false);
                amControler.SetBool("jump", true);
            }
            #endregion
        }
        private void LateUpdate()
        {
            if (CurrentState ==Hero_states.jump)
            {
                jumpHeight += jumpVelocity * Time.deltaTime * jumpSpeed;
                jumpVelocity = jumpVelocity - 9.8f * Time.deltaTime * jumpSpeed;
                Vector3 currentPosition = new Vector3(transform.position.x,transform.position.y,transform.position.z);
                currentPosition.y = jumpHeight;
                transform.position = currentPosition;
                
            }
        }
        private void OnTriggerEnter2D(Collider2D collision)
        {
             #region
            if (collision.gameObject.tag == "Ground")
            {
                amControler.SetBool("jump", false);
                if (Input.GetAxisRaw("Horizontal")!=0)
                {
                    CurrentState = Hero_states.run;
                    amControler.SetBool("run", true);
                }
                else
                {
                    CurrentState = Hero_states.stand;
                    amControler.SetBool("stand", true);
                }
                jumpVelocity = 5f;
                Debug.Log("ground!");
            }
            #endregion
        }

    但是存在角色调到地里面的情况,有待改进。

    3.不使用刚体 (可能是最好的解决办法)使用 Raycast 是一个很好的选择。

    还没实现,挖个坑。

  • 相关阅读:
    在linux下Ant的环境配置
    在linux下Java的环境配置
    CSS列表逆序
    HTML元素基础学习
    第一天---HTML基础学习
    排球计分程序
    罗辑思维:怎样成为一个高手 观后感
    十八周个人作业
    本周个人作业
    个人作业
  • 原文地址:https://www.cnblogs.com/micro-universe/p/13616772.html
Copyright © 2011-2022 走看看