zoukankan      html  css  js  c++  java
  • unity纯粹物理驱动方式

    首先见官方文档

    In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical example where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity.

    不要在每一帧中改变物体的刚体速度,否则会导致不真实的物理效果,比如如下代码,实际运行时,角色会发生严重的抖动

    void FixedUpdate()
        {
            float horizon=input.getAix("Hozizontal");
         rigidebody.Velocity= new Vector2(maxSpeed*horizon,rigidbody.velocity.y);
        }
    或者
    void FixedUpdate()
        {
            float horizon=input.getAix("Hozizontal");
         rigidebody.Addforce(new vector2(maxForce*horizon));
        }

    因此不能在帧运行过程中做出任何改变速度的行为,最好的方式是使用物理引擎本身的持续力来实现对物体的推动

    //正常运动状态:跳跃,走动
    void NormalMove() {
    float j = Input.GetAxis("Jump");
    if (grounded && (Input.GetButtonUp("Jump") || j > 0.99f))
    {
    rigid2D.AddForce(new Vector2(0, j * jumpForce));
    }

    if (Input.GetKey(KeyCode.D))
    {
    if (rigid2D.velocity.x < maxSpeed.x)                                       //如果运动的速度小于最大速度,那么
    constantForce2D.force = new Vector2(horizonForce, 0);       //让持续的推动力为预制的力
    else constantForce2D.force = new Vector2(0, 0);                  //如果速度大于等于最大速度,那么让推动力为0
    }
    else if (Input.GetKey(KeyCode.A))
    {
    if (rigid2D.velocity.x > -maxSpeed.x)
    constantForce2D.force = new Vector2(-horizonForce, 0);
    else constantForce2D.force = new Vector2(0, 0);
    }
    else constantForce2D.force = new Vector2(0, 0);

    //如果不想让角色水平运动,那么设置角色减速,否则让阻力为0,表示角色掉落或者在运动过程中
    if (grounded && !(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKeyDown(KeyCode.Space))&&!lockIgnore) rigid2D.drag = 20f;
    else rigid2D.drag = 0f;
    }

    物体的组件设置如下,其中物理材质的属性全部设置为0,物理检测为continues,睡眠模式为never sleep,interpolate为Interpolate

  • 相关阅读:
    刚开发的游戏《天黑请闭眼》
    用手机控制服务器
    专业网站打包/解包asp工具(E文精装版本)!
    令我爱慕的女子(转自7di.net)
    8088 汇编速查手册
    Asp调用函数是否会影响性能?
    文档管理器
    ubuntu install xxx.deb
    Java线程池的原理及几类线程池的介绍
    ubuntu download file path
  • 原文地址:https://www.cnblogs.com/xiaoahui/p/10068859.html
Copyright © 2011-2022 走看看