【发布时间】:2018-05-30 20:47:29
【问题描述】:
我是 Unity 新手,并开始创建一个简单的曲折游戏。平台最初在 X 和 Z 方向生成,玩家在 X 和 Z 方向点击屏幕时改变方向。
在生成 30 个平台之后,我将平台生成切换到 -X 和 Z 轴。我如何通知我的 3d 模型现在在屏幕上点击时,它必须改变 -X 和 Z 轴的方向。
我已经编写了代码,但是当方向切换发生时,我无法告诉我的角色。所以碰巧平台仍然在 X 和 Z 轴上生成,但我的角色现在在 -X 和 Z 轴上移动。
如何使平台生成和角色方向同步?
代码
这是平台生成的代码
void SpawnPlatform() {
if(score < 30)
{
int rand = Random.Range(0, 6);
if (rand < 3)
{
SpawnX();
}
else if(rand >= 3)
{
SpawnZ();
}
}
if(score > 30)
{
int rand = Random.Range(0, 6);
if (rand < 3)
{
SpawnNegX();
}
else if(rand >= 3)
{
SpawnZ();
}
}
void SpawnX()
{
Vector3 pos = lastPos;
pos.x += size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
int rand = Random.Range(0, 4);
if (rand < 1)
{
Instantiate(diamonds, new Vector3(pos.x,pos.y+1,pos.z),
diamonds.transform.rotation);
}
}
void SpawnNegX()
{
Vector3 pos = lastPos;
pos.x -= size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
int rand = Random.Range(0, 4);
if (rand < 1)
{
Instantiate(diamonds, new Vector3(pos.x,pos.y+1,pos.z),
diamonds.transform.rotation);
}
}
void SpawnZ()
{
Vector3 pos = lastPos;
pos.z += size;
lastPos = pos;
Instantiate(platform, pos, Quaternion.identity);
int rand = Random.Range(0, 4);
if (rand < 1)
{
Instantiate(diamonds, new Vector3(pos.x, pos.y + 1, pos.z),
diamonds.transform.rotation);
}
}
这用于角色控制器。问题就在这里。我如何通知现在在 -X 和 Z 而不是 X 和 Z 之间切换方向的角色。
void Update () {
if (!started)
{
if (Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector3(speed, 0, 0);
}
}
if (!Physics.Raycast(transform.position, Vector3.down, 1f))
{
rb.velocity = new Vector3(0, -25f, 0);
}
if (Input.GetMouseButtonDown(0) && !gameOver)
{
SwitchDirection();
}
}
void SwitchDirection()
{
if (rb.velocity.z > 0)
{
rb.velocity = new Vector3(speed, 0, 0);
}else if(rb.velocity.x > 0)
{
rb.velocity = new Vector3(0, 0, speed);
}
}
我正在复制经典的曲折游戏,但也在其他方向上生成平台。所以它现在看起来像原来的游戏。
【问题讨论】:
-
你是不是用这个函数来切换方向SwitchDirection()?在 Spawning 代码的哪几行之后,您想执行此切换?我给了你一个关于如何从一个脚本调用一个方法到另一个脚本的答案,但是为了让它正常工作,我需要确切地知道你在何时何地实现这个方向切换。
-
@IgnacioAlorre 说前 30 个平台是在 -x 方向生成的。所以角色在点击时移动 -x 。从 31 到 60,平台在 X 方向生成。所以第 31 个平台是应该通知角色切换的时间。