C# 如何同时移动每个游戏对象?

C# 如何同时移动每个游戏对象?,c#,unity3d,visual-studio-code,C#,Unity3d,Visual Studio Code,我正在创建一个基于网格的节奏游戏,灵感来自Necrodancer的地下室。 为此,我必须同时移动玩家和敌人(我的情况是鸡) 我创建了一个GameLogic脚本,其中计时器正倒计时到零: public class GameLogic : MonoBehaviour { public float maxTime = 0.5f; public float timeLeft; public bool move; void Start() {

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

我创建了一个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;

    }
公共类游戏逻辑:单一行为
{
公共浮动最大时间=0.5f;
公共浮动时间限制;
公共布尔移动;
void Start()
{
timeLeft=maxTime;
}
void LateUpdate()
{
//通用定时器
timeLeft-=Time.deltaTime;

如果(timeLeft你在这里基本上拥有的是一个基于回合的游戏。亡灵舞者的地穴是一个类似盗贼的,基于回合是游戏盗贼的一个重要属性

通常游戏被分成叫做单位的“游戏滴答声”。一个“实时”游戏,只有非常高的游戏滴答声(每秒30-60次)这不会等待用户输入完成回合。通常情况下,这更多是关于限制GameTicks到上限,并确保不会减慢绘图速度。GameTicks应始终在更新中计数和处理,而不是绘图代码

现在的问题是,现在的滴答声不仅仅是计算回合数。还有其他一些东西,比如动画的某些部分需要计时。或者解释与播放无关的用户输入(菜单)我可以说,在不知道的情况下,Unity将提供一个游戏计时计数器。无论是作为参数更新还是作为类字段更新

所以你有两个选择:

  • 在游戏回合中使用滴答计数。但是不要进行输入处理。基本上只有更新的移动部分在滴答限制/计数下
  • 像往常一样限制滴答声的计数,但是要有一个单独的计数器。一个你作为更新代码的子部分在gameticks下前进的计数器
  • 对于这个特殊的情况,可能会有Unity支持。如果是,通常使用那个

  • 这里基本上是一个基于转机的游戏。Necrodancer的地下室是一个类似盗贼的地方,转机是游戏盗贼的一个重要属性

    通常游戏被分成叫做单位的“游戏滴答声”。一个“实时”游戏,只有非常高的游戏滴答声(每秒30-60次)这不会等待用户输入完成回合。通常情况下,这更多是关于限制GameTicks到上限,并确保不会减慢绘图速度。GameTicks应始终在更新中计数和处理,而不是绘图代码

    现在的问题是,现在的滴答声不仅仅是计算回合数。还有其他一些东西,比如动画的某些部分需要计时。或者解释与播放无关的用户输入(菜单)我可以说,在不知道的情况下,Unity将提供一个游戏计时计数器。无论是作为参数更新还是作为类字段更新

    所以你有两个选择:

  • 在游戏回合中使用滴答计数。但是不要进行输入处理。基本上只有更新的移动部分在滴答限制/计数下
  • 像往常一样限制滴答声的计数,但是要有一个单独的计数器。一个你作为更新代码的子部分在gameticks下前进的计数器
  • 对于这个特殊的情况,可能会有Unity支持。如果是,通常使用那个
  •  GameLogic gameLogic;
    
        Vector3 up = Vector3.zero,
        right = new Vector3(0, 90, 0),
        down = new Vector3(0, 180, 0),
        left = new Vector3(0, 270, 0),
        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, destination, 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, 0), 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;
        }
    }
    
    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();
    
            }
        }
    }