我正在创建一个基于网格的节奏游戏,灵感来自
我创建了一个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();
}
}
}
我对任何建议感到高兴!预先谢谢你:)