2017-02-24 85 views
0

我想做的是在Unity中制作一种2.5D亚军游戏,该角色的所有三个旋转轴都被冻结, Z轴也被冻结。我不知道如何让角色在跷跷板上向前移动。 (我使用HingeJoint创建跷跷板。)Unity3D - 让人物在2.5D亚军游戏中的跷跷板上向前移动

我创建了一个通过使用Physics.Raycast()函数检测CapsuleCollider状态的结构,并且工作正常。

private struct ColliderStatus 
    { 
     public bool headed; //colliding up 
     public bool footed; //colliding down 
     public bool onPlane; //colliding down && the obstacle colliding does not have slope angle 
     public bool lefted; //colliding left 
     public bool righted; //colliding right 
     public bool inAir; //not colliding anything 
    } 

我尝试以下方法:

  1. 刚体上添加力向前

    //To move character rigidbody move forward automatically in runner game 
    //when the speed is lower than the minimum speed and it's on plane or in air. 
    if (rigidbody.velocity.x < minForwardSpeed && (colliderStatus.onPlane || colliderStatus.inAir)) 
    { 
        rigidbody.AddForce(20f * Vector3.right); 
    } 
    
    //Add gravity to player 
    Vector3 gravityForce = new Vector3(0f, -gravityOnPlayer, 0f); 
    rigidbody.AddForce(gravityForce); 
    

因为性格继续往上走它不能很好地工作尽管跷跷板开始倾斜,但当它在跷跷板上时。当角色从高空坠落到地面或跳跃后会出现速度损失,看起来角色会在着陆点上愣住一会儿,然后开始加速。

  • 使用transform.Translate()向前移动& &变化增加重力

    //Use transform.Translate() to move forward 
    //I recognize that by this way, there will be no velocity loss 
    //when the character falling down to the ground at the landing point 
    //If I don't use this condition, my character will stuck on the 
    //right vertical wall 
    if (!colliderStatus.righted) 
    { 
        transform.Translate(new Vector2(minForwardSpeed, 0f) * Time.deltaTime); 
    } 
    
  • 的方式,我不知道为什么我可以” t这样写,因为它会导致速度不正确反应:

    //Use transform.Translate() to move forward 
        if (!colliderStatus.righted && rigidbody.velocity.x < minForwardSpeed) 
        { 
         transform.Translate(new Vector2(minForwardSpeed, 0f) * Time.deltaTime); 
        } 
    

    要改变添加重力的方式,我使用一个函数在SlopeAngleVector()上计算字符运行的斜率向量。

    private Vector3 SlopeAngleVector() 
        { 
        Vector3 nextStepPositon = new Vector3(transform.position.x + 0.01f, transform.position.y, 0f); 
        Ray nextPosRay = new Ray(nextStepPositon, Vector3.down); 
        Ray nowPosRay = new Ray(transform.position, Vector3.down); 
        RaycastHit nextPosHit; 
        RaycastHit nowPosHit; 
        Vector3 slopeAngle = Vector3.zero; 
    
        Physics.Raycast(nowPosRay, out nowPosHit, 5f, obstaclesLayerMask); 
        if (Physics.Raycast(nextPosRay, out nextPosHit, 5f, obstaclesLayerMask)) 
        { 
         slopeAngle = new Vector3(nextPosHit.point.x - nowPosHit.point.x, nextPosHit.point.y - nowPosHit.point.y, 0f).normalized; 
        } 
        return slopeAngle; 
        } 
    

    然后,我通过计算新增的重力对边坡矢量重力投影:

    private void AddGravity() 
        { 
        Vector3 gravityForce = new Vector3(0f, -gravityOnPlayer, 0f); 
        //my character could be collided by the long vertical wall(colliderStatus.righted) 
        //so I set the condition as "!colliderStatus.footed" 
        //otherwise, I would use "colliderStatus.inAir" 
        if (!colliderStatus.footed) 
        { 
         gravityForce = new Vector3(0f, -gravityOnPlayer, 0f); 
        } 
        else 
        { 
         gravityForce = Vector3.Project(Vector3.down * gravityOnPlayer, SlopeAngleVector()); 
        } 
        rigidbody.AddForce(gravityForce); 
        } 
    

    现在我的性格可以从跷跷板滑下,但它会继续退步。当它在低斜角跷跷板上时不能通过。

    如何在跷跷板上为跑步者制作出好的行为脚本?

    +0

    您是否尝试过改变速度而不是增加强制力? –

    回答

    0

    我建议看看一些Unity标准资产角色控制器,我相信他们会考虑角色移动的斜率。它可能会给你一些想法。

    我还建议修改代码计算斜率角度的方式。 raycast命中会让你回到曲面法线,那么你应该可以使用Vector3.Cross来计算斜率的角度。

    它会像这样:Vector3.Cross(正常,(指向屏幕的矢量))。

    您可能需要对其进行调整以使其正常工作,但这可以为您提供一个光线投射中的倾斜角度。它也可能会消除您的位置位于锯床下方的潜在问题。

    作为一般技巧,尽量不要将变换和刚体混合在一起,如果要移动刚体,直接移动刚体,而不是间接通过变换。