• 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

  • 相关阅读:
    python活力练习Day13
    检测一个字符串在另外一个字符串中的位置
    Python活力练习Day12
    Python多进程与单进程效率对比
    HTML-Note
    Python判断自定义的参数格式是否正确
    图片的灰与彩
    Git常用命令
    Linux 单引号和双引号的区别
    类函数中获取进程池对象的地址
  • 原文地址:https://www.cnblogs.com/xiaoahui/p/10068859.html
Copyright © 2020-2023  润新知