Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
统一及;C#-汽车在坡道上行驶有点不平稳_C#_Unity3d - Fatal编程技术网

统一及;C#-汽车在坡道上行驶有点不平稳

统一及;C#-汽车在坡道上行驶有点不平稳,c#,unity3d,C#,Unity3d,我正在做一个赛车游戏。当我制造的汽车在坡道上行驶时,它会变得有点紧张。不要认为这是相机的问题,因为游戏在平地上和下坡时看起来很平稳。 这是一段汽车在坡道上抖动的视频: 我遵循了本教程: 我注意到在本教程中,作者的最终结果也有点紧张。 代码如下: { sphereRb.transform.parent = null; } private void Update() { speedInput = 0; if (Inp

我正在做一个赛车游戏。当我制造的汽车在坡道上行驶时,它会变得有点紧张。不要认为这是相机的问题,因为游戏在平地上和下坡时看起来很平稳。 这是一段汽车在坡道上抖动的视频:

我遵循了本教程:

我注意到在本教程中,作者的最终结果也有点紧张。 代码如下:

    {
        sphereRb.transform.parent = null;
    }

    private void Update()
    {
        speedInput = 0;
        if (Input.GetAxis("Vertical") > 0)
        {
            GetInputForMoving(forwardAccel);
        }
        else if (Input.GetAxis("Vertical") < 0)
        {
            GetInputForMoving(reverseAccel);

        }

        turnInput = Input.GetAxis("Horizontal");

        if (grounded)
        {
            Turning();
        }

        SetWheelsRotation();

        transform.position = sphereRb.transform.position;
    }

    


    void GetInputForMoving(float directionAccel)
    {
        speedInput = Input.GetAxis("Vertical") * directionAccel * multiplier;
    }

    void Turning()
    {
        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + new Vector3(0f, turnInput * turnStrength * Time.deltaTime, 0f));

    }

    void SetWheelsRotation()
    {
        leftFrontWheel.localRotation = Quaternion.Euler(leftFrontWheel.localRotation.eulerAngles.x, (turnInput * maxWheelTurn) - 180, leftFrontWheel.localRotation.eulerAngles.z);
        rightFrontWheel.localRotation = Quaternion.Euler(rightFrontWheel.localRotation.eulerAngles.x, (turnInput * maxWheelTurn), rightFrontWheel.localRotation.eulerAngles.z);

    }
    private void FixedUpdate()
    {
        grounded = false;
        RaycastHit hit;


        //if hits ground
        if (Physics.Raycast(groundRayPoint.position, -transform.up, out hit, groundRayLength, whatisGround))
        {
            grounded = true;

            transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
        }

        if (grounded)
        {
            Moving();
        }
        else
        {
            Dropping();
        }

        
    }

    void Moving()
    {
        sphereRb.drag = dragOnGround;
        if (Mathf.Abs(speedInput) > 0)
        {
            sphereRb.AddForce(transform.forward * speedInput);
        }
    }

    void Dropping()
    {
        sphereRb.drag = 0.1f;
        sphereRb.AddForce(Vector3.up * -gravityForce * 100);
    }
{
sphereRb.transform.parent=null;
}
私有void更新()
{
速度输入=0;
if(Input.GetAxis(“垂直”)>0)
{
GetInputForMoving(forwardAccel);
}
else if(Input.GetAxis(“垂直”)<0)
{
GetInputForMoving(reverseAccel);
}
turnInput=Input.GetAxis(“水平”);
如果(接地)
{
转动();
}
设置车轮旋转();
transform.position=sphereRb.transform.position;
}
void GetInputForMoving(浮点方向Accel)
{
speedInput=Input.GetAxis(“垂直”)*方向加速*乘数;
}
空转()
{
transform.rotation=Quaternion.Euler(transform.rotation.eulerAngles+new Vector3(0f,turnInput*turnStrength*Time.deltaTime,0f));
}
无效设置车轮旋转()
{
leftFrontWheel.localRotation=Quaternion.Euler(leftFrontWheel.localRotation.eulerAngles.x,(turnInput*maxWheelTurn)-180,leftFrontWheel.localRotation.eulerAngles.z);
rightFrontWheel.localRotation=Quaternion.Euler(rightFrontWheel.localRotation.eulerAngles.x,(turnInput*maxWheelTurn),rightFrontWheel.localRotation.eulerAngles.z);
}
私有void FixedUpdate()
{
接地=假;
雷卡斯特击中;
//如果击中地面
if(Physics.Raycast(groundRayPoint.position,-transform.up,out-hit,groundRayLength,whatisGround))
{
接地=正确;
transform.rotation=四元数fromtrotation(transform.up、hit.normal)*transform.rotation;
}
如果(接地)
{
移动();
}
其他的
{
滴落();
}
}
无效移动()
{
sphereRb.drag=龙地;
如果(数学Abs(速度输入)>0)
{
sphereRb.AddForce(transform.forward*速度输入);
}
}
空投()
{
sphereRb.drag=0.1f;
sphereRb.AddForce(矢量3.up*-重力力*100);
}

请帮助我,我真的很挣扎。对不起我的英语,提前谢谢

实际上,汽车的运动看起来很好,而且看起来很平稳

真正的问题是汽车的旋转

如果你看这一行:

transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
您是在告诉汽车根据方向对齐其y轴。 然而。多边形正在塑造坡度,坡度的每个“部分”之间的法线会突然变化:这就是汽车沿坡度旋转如此陡峭的原因。通过启用线框着色模式(仅在“场景”选项卡下可用的选项),可以在场景视图中看到它

您可以使用如下代码在当前汽车旋转和目标旋转之间插值:

Quaternion toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;

float angle = Vector3.Angle(transform.up, hit.normal);

transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, angle * Time.fixedDeltaTime * rotateSpeed);
你必须调整旋转速度,直到它符合你的感觉。
将以每秒角度x旋转速度在当前旋转和目标旋转之间平滑插值。有更好的方法来完成所有这些,但我希望这将为您解锁您目前拥有的功能。

汽车的运动实际上看起来很好,而且看起来很平稳

真正的问题是汽车的旋转

如果你看这一行:

transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
您是在告诉汽车根据方向对齐其y轴。 然而。多边形正在塑造坡度,坡度的每个“部分”之间的法线会突然变化:这就是汽车沿坡度旋转如此陡峭的原因。通过启用线框着色模式(仅在“场景”选项卡下可用的选项),可以在场景视图中看到它

您可以使用如下代码在当前汽车旋转和目标旋转之间插值:

Quaternion toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;

float angle = Vector3.Angle(transform.up, hit.normal);

transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, angle * Time.fixedDeltaTime * rotateSpeed);
你必须调整旋转速度,直到它符合你的感觉。
将以每秒角度x旋转速度在当前旋转和目标旋转之间平滑插值。有更好的方法来完成所有这些,但我希望这将用您目前拥有的解锁您。

第一行应该是“void Start()”,很抱歉,我没有在上面的代码中包含它。我在视频中没有发现它在坡道上抖动……如果您转到视频的31:42,并以0.25倍的速度播放,您会看到它抖动。这在这段视频中不是问题,但因为我希望我的相机离汽车更近,所以很容易发现这种不安。同意@rustyBucketBay。。我真的不明白你到底指的是什么。。如果将视频速度设置为0.25倍,则所有内容都会出现抖动^^。。你能为我们更清楚地定义一下神经过敏吗?谢谢大家的评论。对不起,我应该说得更清楚。我上传了上面坡道上汽车抖动运动的视频,请看一看。第一行应该是“void Start()”,很抱歉我没有在上面的代码中包含它。我在视频中没有发现它在坡道上抖动……如果你转到视频的31:42,并以0.25倍的速度播放,你会看到它抖动。这在这段视频中不是问题,但因为我希望我的相机离汽车更近,所以很容易发现这种不安。同意@rustyBucketBay。。我真的不明白你到底指的是什么。。如果将视频速度设置为0.25倍,则所有内容都会出现抖动^^。。你能为我们更清楚地定义一下神经过敏吗?谢谢大家的评论。对不起,我应该