0

我想知道如何在不使用角色控制器的情况下在 Unity 中进行球跳跃。球需要有一个用于重力的刚体和一个脚本,以使其在球的相对方向上跳跃。有人可以帮忙吗?

4

1 回答 1

0

正如 skovacs1在 Unity Answers 上所说

简单的答案是调整 inAirControlAcceleration 的值,它应该给你的东西更像你想象的那样。为了获得更多控制,您还可以将复合分配替换 movement.inAirVelocity

movement.inAirVelocity +=
    movement.direction * Time.deltaTime *
    movement.inAirControlAcceleration 

以便该值按 h 的输入量进行缩放。

如果这仍然不能满足您的需求,假设您使用的是链接的确切脚本(因为您没有发布任何其他内容),那么这里是 Update 中的相关功能代码所做的:

//move horizontally as calculated in UpdateSmoothedMovementDirection * speed
//+ vertically by the speed calculated in ApplyGravity or ApplyJumping
//+ horizontally + vertically by the inAirMovement calculated in ApplyJumping
//+ horizontally by the adjustment to inAirMovement calculated in
//UpdateSmoothedMovementDirection 
var currentMovementOffset = movement.direction * movement.speed
                            + Vector3(0,movement.verticalSpeed,0)
                            + movement.inAirVelocity;
currentMovementOffset *= Time.deltaTime;
movement.collisionFlags = controller.Move (currentMovementOffset);

您似乎只关心水平运动,因此您真正需要关心的唯一变量是motion.direction、motion.speed 和motion.inAirVelocity。

完成工作的部分:

function UpdateSmoothedMovementDirection() { //called by Update every frame
    //...irrelevant stuff
    //move direction is *always* (canControl is never changed in this script)
    //the horizontal input when the absolute horizontal input is greater than 0.1
    if (movement.isMoving) movement.direction = Vector3 (h, 0, 0);

    //calculate the speed change only when on the ground
    if (controller.isGrounded) {
        var curSmooth = movement.speedSmoothing * Time.deltaTime;
        var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0);
        if(Input.GetButton("Fire2") && canControl)
            targetSpeed *= movement.runSpeed;
        else
            targetSpeed *= movement.walkSpeed;

        movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);
        //...irrelevant stuff
    }
    //inAirVelocity is going to be adjusted horizontally by Time.deltaTime
    //in the direction of horizontal input
    else {
        //...irrelevant stuff
        if (movement.isMoving)
            movement.inAirVelocity += Vector3(Mathf.Sign(h),0,0) * Time.deltaTime
                                      * movement.inAirControlAcceleration;
    }
}

设置初始 inAirVelocity 的部分:

function ApplyJumping() { //called by Update every frame
    //...irrelevant stuff
    //When on the ground and we can jump, the inAirVelocity will be 
    //the motion of the platform we're on
    if (controller.isGrounded) {
        if (jump.enabled && Time.time < jump.lastButtonTime + jump.timeout) {
            //...irrelevant stuff
            movement.inAirVelocity = lastPlatformVelocity;
            //...irrelevant stuff
        }
    }
}

可能出乎您的意料的是,您只在地面上计算速度。

一旦在空中,你会以你最后一次在地面上时的速度在输入方向 ( movement.direction) + 受输入方向影响的一些调整 ( inAirVelocity) 移动。

我认为这应该以某种方式模拟惯性,但它真的不起作用,因为你仍然可以改变方向并且没有减速。

要获得正确的惯性方向,您需要将更改移动到 motion.direction 到后面的if(Controller.isGrounded)块中。要获得实际的惯性减速度,您可以在 else 语句中以某个平滑速率减去 speed ,直到它为 0 并保持与lastPlatformVelocity分开inAirVelocity,将其添加为单独的项并使其减速。

为了在空中进行全速控制,我建议您将速度的东西从 if 块中移出(在这种情况下,inAirVelocity可能不需要在 中更改UpdateSmoothedMovementDirection)。

或者,您可以通过执行类似的操作来更改跳跃时用来计算运动的方法

if(!controller.isGrounded) movement.speed = inAirSpeed;

但这会导致一些不自然的行为,您可能需要在空中进行一些调整。

于 2011-03-28T09:58:48.733 回答