我有 2 个游戏对象,一个蓝色和一个红色。两人都在场景中跑来跑去,镜头受限,所以他们不能通过某个区域。
如果可能的话,我也想知道如何在对角线上移动它们。
这是我的全部代码。
public Transform Objectup;
public Transform Objectdown;
public Transform Objectright;
public Transform Objectleft;
public DirectionGameObject direction;
public int speed = 2;
public enum DirectionGameObject
{
Right,
Left,
Up,
Down
}
// Start is called before the first frame update
void Start()
{
direction = SortdirectionGameObject(direction);
}
// Update is called once per frame
void Update()
{
Destroy();
if(direction == DirectionGameObject.Right)
{
transform.Translate(Vector3.right * Time.deltaTime * speed);
if(transform.position.x >= Objectright.position.x)
{
direction = SortdirectionGameObject(direction);
}
}
if(direction == DirectionGameObject.Left)
{
transform.Translate(Vector3.left * Time.deltaTime * speed);
if(transform.position.x <= Objectleft.position.x)
{
direction = SortdirectionGameObject(direction);
}
}
if(direction == DirectionGameObject.Up)
{
transform.Translate(Vector3.up * Time.deltaTime * speed);
if(transform.position.y >= Objectup.position.y)
{
direction = SortdirectionGameObject(direction);
}
}
if(direction == DirectionGameObject.Down)
{
transform.Translate(Vector3.down * Time.deltaTime * speed);
if(transform.position.y <= Objectdown.position.y)
{
direction = SortdirectionGameObject(direction);
}
}
}
private DirectionGameObject SortdirectionGameObject(DirectionGameObject maindirection)
{
DirectionGameObject directionSorted = maindirection;
do{
int newDirection = Random.Range((int)DirectionGameObject.Right, (int)DirectionGameObject.Down + 1);
directionSorted = (DirectionGameObject)newDirection;
} while (maindirection == directionSorted);
return directionSorted;
}
void Destroy()
{
Destroy(gameObject,120.0f);
}
当我按下 G 和 V 时,我需要在控制台上显示它们的颜色和位置。
C#unity3d
守候你守候我
相关分类