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 是一个很好的选择。

    还没实现,挖个坑。

  • 相关阅读:
    Cornerstone-忽略(隐藏)文件
    ios开发xcode8+ 无需开发者账号,app打包ipa
    ssh-ajax登陆action返回字符串
    手动编译包含两个import自写类的java类。
    关闭IO资源
    java聊天室二(客户端)
    java聊天室一(服务器)
    文件IO常用操作
    Hive启动时的棘手问题的处理
    对于java反射的理解
  • 原文地址:https://www.cnblogs.com/micro-universe/p/13616772.html
Copyright © 2011-2022 走看看