- Home /
3d Model Movement according to platform spawning
I am new to unity and started creating a simple zigzag game. The platforms initially spawn in the X and Z directions and the player changes direction on screen tap in X and Z directions.
After, lets say, spawning of 30 platforms, i switch the platform spawning to -X and Z axis. How do I inform my 3d model that now on screen tap, it has to change directions in -X and Z axis.
I have written the code but the problem occurs that I am not able to tell my character when the direction switch occurs. So it happens that the platforms are still spawning in X and Z axis, but my character now moves in -X and Z axis.
How do i bring platform spawning and character directions in sync?
Share your code, where you are spawning, and elaborate what is platform?
Here is the code for platform spawning
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);
}
}
and this for the character controller. The issue is here. how do i inform the character that now switch directions between -X and Z ins$$anonymous$$d of X and Z.
void Update () {
if (!started)
{
if (Input.Get$$anonymous$$ouseButtonDown(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.Get$$anonymous$$ouseButtonDown(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);
}
}
I am replicating the classic zigzag game but spawning platforms in the other directions as well. so it looks like the original game for now.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163704im_/https://answers.unity.com/themes/thub/images/avi.jpg)