如何同时移动每个GameObject?

我正在创建一个基于网格的节奏游戏,灵感来自死灵舞者的墓穴。 为此,我必须同时精确移动玩家和敌人(在我的情况下是鸡)。

我创建了一个GameLogic脚本,其中的计时器递减为零:

public class GameLogic : MonoBehaviour
{
    public float maxTime = 0.5f;
    public float timeLeft;

    public bool move;

    void Start()
    {

        timeLeft = maxTime;
    }

    void LateUpdate()
    {
        //Universal Timer
        timeLeft -= Time.deltaTime;

        if(timeLeft <= 0)
        {
            move = true;
            UpdateTime();
        }


    }

    public void UpdateTime()
    {
        timeLeft = maxTime;
        move = false;

    }

现在我在播放器脚本中引用此Timer:

 GameLogic gameLogic;

    Vector3 up = Vector3.zero,right = new Vector3(0,90,0),down = new Vector3(0,180,left = new Vector3(0,270,currentDirection = Vector3.zero;

    Vector3 nextPos,destination,direction;

    float speed = 5;
    float rayLength = 1f;
    //float timeLeft;
    //public float maxTime = 1;



    bool canmove = true;
    //bool movedToNext;
    bool bouncedOff;


    void Start()
    {
        GameObject g = GameObject.Find("Game Logic");
        gameLogic = g.getcomponent<GameLogic>();

        currentDirection = up;
        nextPos = Vector3.forward;
        destination = transform.position;

    }

    // Update is called once per frame
    void Update()
    {

        BeatMovement();
        Move();

    }

    void BeatMovement()
    {
        //timeLeft -= Time.deltaTime;

        if (gameLogic.move)
        {
            canmove = true;
            bouncedOff = false;

            //gameLogic.move = false;

        }
        else { canmove = false; }
    }

    void Move()
    {
        transform.position = Vector3.MoveTowards(transform.position,speed * Time.deltaTime);

        if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            nextPos = Vector3.forward;
            currentDirection = up;
        }
        if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
        {
            nextPos = Vector3.back;
            currentDirection = down;

        }
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            nextPos = Vector3.right;
            currentDirection = right;

        }
        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            nextPos = Vector3.left;
            currentDirection = left;

        }


        if (Vector3.Distance(destination,transform.position) <= 0.000001f)
        {
            transform.localEulerAngles = currentDirection;

            if(canmove)
            {
                if(Valid())
                {
                    destination = transform.position + nextPos;
                    direction = nextPos;

                }
                else
                {
                    if(currentDirection == up && bouncedOff == false)
                    {
                        nextPos = Vector3.back;
                        currentDirection = down;
                        bouncedOff = true;

                    }
                    if (currentDirection == down && bouncedOff == false)
                    {
                        nextPos = Vector3.forward;
                        currentDirection = up;
                        bouncedOff = true;
                    }

                    if (currentDirection == left && bouncedOff == false)
                    {
                        nextPos = Vector3.right;
                        currentDirection = right;
                        bouncedOff = true;
                    }

                    if (currentDirection == right && bouncedOff == false)
                    {
                        nextPos = Vector3.left;
                        currentDirection = left;
                        bouncedOff = true;
                    }
                }
            }
        }
    }

    //checks if there is an obstacle in the destinationPosition the Player wants to move
    bool Valid()
    {
        Ray myRay = new Ray(transform.position + new Vector3(0,0.25f,transform.forward);
        RaycastHit hit;

        Debug.DrawRay(myRay.origin,myRay.direction,Color.red);

        if(Physics.Raycast(myRay,out hit,rayLength))
        {
            if(hit.collider.tag == "Obstacle")
            {
                return false;
            }
        }

        //if the raycast doesnt hit anything the movement is valid (true) 
        return true;
    }
}

这似乎有效。但是,一旦有另一个GameObject使用此Timer,事情就会变得复杂。两个GameObject都不会移动。我认为这是因为计时器不再达到零,并且很快就会更新。我试图更新播放器或ChickeScripts中的时间,但这将导致其中之一无法移动。我可以看到问题,我只是不知道如何解决。

这是使用PathFollower脚本编写的鸡肉代码:

public class PathFollower : MonoBehaviour
{
    Node[] pathNode;
    public GameObject player;

    Vector3 CurrentPositionHolder;
    int CurrentNode;


    GameLogic gamelogic;

    void Start()
    {
        GameObject g = GameObject.Find("Game Logic");

        pathNode = getcomponentsInChildren<Node>();
        gamelogic = g.getcomponent<GameLogic>();

        CheckNode();

    }

    void CheckNode()
    {
        //tells me on which position the next Node is
        CurrentPositionHolder = pathNode[CurrentNode].transform.position;
    }

    void Update()
    {   
        if (gamelogic.move)
        {
            Movement();
        }
    }

    void Movement()
    {

        //moves player to the next position
        if (player.transform.position != CurrentPositionHolder)
        {
            player.transform.position = CurrentPositionHolder;
              Debug.Log("moves");

        }
        else
        //if player is on the position u want him to be then set the next node 
        {
            if (CurrentNode < pathNode.Length - 1)
            {
                CurrentNode++;
                CheckNode();

            }
        }

        if (CurrentNode == pathNode.Length -1)
        {
            CurrentNode = 0;
            CheckNode();

        }
    }
}

我对任何建议感到高兴!预先谢谢你:)

qq378755837 回答:如何同时移动每个GameObject?

这里基本上是回合制游戏。死灵巫师的地穴是类盗贼,回合制是游戏Rogue的重要属性。

通常,游戏分为“ GameTicks”(称为单元)。一个“实时”游戏,只有非常高的GameTicks(每秒30-60次),它不等待用户输入完成回合。通常,更多的是将GameTicks限制在一个上限,并确保您不会拖慢绘制速度。应始终在更新中计算和处理游戏标记,而不是在抽奖代码中进行计数。

现在的问题是,如今的价格变动幅度更多,而只是计算转数。还有其他一些东西,例如动画的某些部分需要时间。或解释与播放无关的用户输入(菜单)。我对此一无所知,可以说Unity将提供Game Tick计数器。要么将Update作为参数重载,要么作为类字段重载。

所以您有两个选择:

  1. 使用滴答声计数游戏回合。但是请不要进行输入处理。基本上,只有更新的移动部分才处于刻度限制/计数中
  2. 像往常一样限制刻度线计数,但将转弯数作为单独的计数器。您可以根据自己的意愿将自己的更新作为更新代码的一部分。
  3. 此特定情况可能会有Unity支持。如果是这样,通常使用那个。
本文链接:https://www.f2er.com/3129522.html

大家都在问