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

  • 相关阅读:
    Oracle中的substr()函数和INSTR()函数和mysql中substring_index函数字符截取函数用法:计算BOM系数用量拼接字符串*计算值方法
    (转载)SDRAM驱动笔记
    【转】Verilog阻塞与非阻塞赋值使用要点
    【转转】(筆記) always block內省略else所代表的電路 (SOC) (Verilog)
    (原創) 如何處理signed integer的加法運算與overflow? (SOC) (Verilog)
    [转载]亚稳态
    Dev Exprss 发布部署
    Dev splliter 去除中间的分割显示
    DevTreeList中的新增、修改的设计
    Oracle 常用网址
  • 原文地址:https://www.cnblogs.com/xiaoahui/p/10068859.html
Copyright © 2011-2022 走看看