【问题标题】:Trying endless runner game in unity 3d with 5Lane at place of 3Lane在统一 3d 中尝试用 5Lane 代替 3Lane 进行无尽的跑步游戏
【发布时间】:2021-06-04 09:28:01
【问题描述】:

这是我试图编辑的代码,但是 if else 循环下的条件没有执行任务。在此代码中,lan​​edistance 是常量浮点变量,值为 2.0f。如前所述,它是 5lane 无尽的跑步者,因此所需的车道被视为 int 变量,中间值为 2(Ext. Left = 0, Left = 1, Middle = 2, Right = 3, Ext. Right = 4)---- -------------

// Start is called before the first frame update
void Start()
{
    controller = GetComponent<CharacterController>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.LeftArrow))
        MoveLane(false);
    if (Input.GetKeyDown(KeyCode.RightArrow))
        MoveLane(true);

    Vector3 targetposition = transform.position.z * Vector3.forward;
    if (desiredLane == 0)
    {   
        targetposition += Vector3.left * laneDistance; 
    }

    else if (desiredLane == 1)
    {
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {   //Debug.Log("1i condition");
            MoveLane(true);
            targetposition += Vector3.left * laneDistance;
        }

        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {   
            //Debug.Log("1e condition");
            MoveLane(false);
            targetposition += Vector3.right * laneDistance;
        }
    }

    else if (desiredLane == 3)
    {
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {   
            //Debug.Log("3i condition");
            MoveLane(true);
            targetposition += Vector3.left * laneDistance;
        }

        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {   //Debug.Log("3e condition");
            MoveLane(true);
            targetposition += Vector3.right * laneDistance;
        }
    }

    else if (desiredLane == 4)//                                                    ---------------------- 
        targetposition += Vector3.right * laneDistance;





    Vector3 moveVector = Vector3.zero;
    moveVector.x = (targetposition - transform.position).normalized.x * speed;
    moveVector.y = -0.1f;
    moveVector.z = speed;

    controller.Move(moveVector * Time.deltaTime);
}

private void MoveLane(bool goingRight)
{
    desiredLane += (goingRight) ? 5 : -1;//                                                                 ------------------
    desiredLane = Mathf.Clamp(desiredLane, 0, 4);//                                                         ------------------
    Debug.Log("Value is: "+desiredLane);
}

}

【问题讨论】:

  • 我会检查这一行:desiredLane += (goingRight) ? 5 : -1;// 。我不太了解您的换道逻辑,我希望这些值会改变 1 或 2 个单位,而不是 5
  • @rustyBucketBay 好吧,我昨晚试过了......它没有产生影响。
  • 你尝试了什么?如果 else 语句没有被执行,就是没有满足 else 的条件。你只需要用断点debug你的代码并找出原因
  • 我试过了.. else 两种条件下的语句都不起作用,如果我删除 (desiredLane == 1) 和 else if (desiredLane == 3) 条件,玩家可以轻松地在 3lanes 中工作......

标签: c# unity3d


【解决方案1】:

我猜你的代码可以简化很多。

另外,为什么你总是在没有任何检查的情况下处理前两个GetKeyDown 语句?而desiredLane == 2的情况呢?

你为什么使用

desiredLane += (goingRight) ? 5 : -1

应该是这样

desiredLane += (goingRight) ? 1 : -1

我会说你宁愿做类似的事情

// Configure these two vis the Inspector!
public float sidewardsSpeed;
public float forwardSpeed;

// Not necessary but this way you can give your checks handy names
private bool CanMoveLeft => desiredLane > 0;
private bool CanMoveRight => desiredLane < 4;

float targetPositionX;

void Start()
{
    targetPositionX = transform.position.x;
}

void Update()
{
    // This is for the sidewards movement
    // It's enough to simply check the value of your index ONCE instead of 
    // covering each possible case ;)
    if (CanMoveLeft && Input.GetKeyDown(KeyCode.LeftArrow))
        MoveLane(false);
    else if (CanMoveRight && Input.GetKeyDown(KeyCode.RightArrow))
        MoveLane(true);

    // Finally calculate a smoothed movement on the individual axes
    var currentPosition = transform.position;
    var deltaX = targetPositionX - currentPosition.x;
    var x = Mathf.Min(deltaX, Mathf.Sign(deltaX) * sidewardsSpeed * Time.deltaTime);
    var y = -0.1f * Time.deltaTime;
    var z = forwardSpeed * Time.deltaTime;

    // And then apply both movements 
    controller.Move(new Vector3(x, y, z));
}    

private void MoveLane(bool goingRight)
{
    var direction = goingRight ? 1 : -1;
    desiredLane += direction; 
    desiredLane = Mathf.Clamp(desiredLane, 0, 4);                         
    Debug.Log("Value is: "+desiredLane);
 
    // Also directly calculate the target X position     
    targetPositionX = (desiredLane - 2) * laneDistance;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多