【发布时间】:2020-03-30 19:32:20
【问题描述】:
我试图让一个对象在 Unity 中每秒移动一次,但它似乎不起作用。我正在尝试制作游戏蛇,我首先将头部的 Sprite 居中,然后每秒将其向右移动,然后添加玩家控件。 有什么帮助让它工作吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Snake_Move : MonoBehaviour
{
private Vector2 pos;
private Vector2 moveDirection;
private float moveTimer;
private float timerSeconds;
private void Startup()
{
pos = new Vector2(5, 5);
timerSeconds = 1f;
moveTimer = timerSeconds;
moveDirection = new Vector2(1, 0);
}
private void Update()
{
moveTimer += Time.deltaTime;
if (moveTimer > timerSeconds)
{
pos += moveDirection;
moveTimer -= timerSeconds;
}
transform.position = new Vector2(pos.x, pos.y);
}
}
【问题讨论】: