Reputation: 164
I'm trying to make a semi-procedural generated map on unity for a game I'm developing. In the game, square platforms are spawned in every direction when the player moves towards them (North, South, East, West, NWest, NEast, SWest, SEast). These platforms contain a flat Terrain as ground so I could paint textures on them and what not. To give the game some diversity I intend to pick a random rotation for these platforms when they spawn (90, 180 and 270 degrees).
The problem is, the pivot point of a terrain is not in the center of it, by default, leading to a really awkward and, in this case, destructive result. I tried to child it to an empty game object with a pivot on the center, but I get the same results.
Does anyone know a way to overcome this problem? I know it is not quite a coding problem, but it's a technical one none the less. I've searched around and the common solution is to child it to an empty, but as I said, it does not work in this case, unless I'm missing something. As always, thank you in advance for your patience and attention. Rui Pires
Note: This is my rotating script, in case there's something I can achieve by modifying it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoRotate : MonoBehaviour
{
Quaternion rot1;
private int a;
private void Awake()
{
a = Random.Range(0, 4);
rot1 = transform.rotation;
}
// Start is called before the first frame update
void Start()
{
switch(a)
{
case 0: transform.rotation = rot1;
Debug.Log("case 0");
break;
case 1: rot1 = Quaternion.AngleAxis(90, Vector3.up);
transform.rotation = rot1;
Debug.Log("case 1");
break;
case 2: rot1 = Quaternion.AngleAxis(180, Vector3.up);
transform.rotation = rot1;
Debug.Log("case 2");
break;
case 3:
rot1 = Quaternion.AngleAxis(270, Vector3.up);
transform.rotation = rot1;
Debug.Log("case 3");
break;
}
}
}
Edit:
Adding a couple of images to better portray the problem.
Upvotes: 2
Views: 2012
Reputation: 164
I ended up with a "bandaid" solution adding a localPosition value to the terrain to correct the rotation behaviour:
Quaternion rot1;
private int a;
private void Awake()
{
a = Random.Range(0, 4);
rot1 = transform.rotation;
}
// Start is called before the first frame update
void Start()
{
switch(a)
{
case 0: transform.rotation = rot1;
Debug.Log("case 0");
break;
case 1: rot1 = Quaternion.AngleAxis(90, Vector3.up);
transform.rotation = rot1;
transform.GetChild(0).transform.localPosition = new Vector3(transform.GetChild(0).transform.localPosition.x + 100, transform.GetChild(0).transform.localPosition.y, transform.GetChild(0).transform.localPosition.z);
Debug.Log("case 1");
break;
case 2: rot1 = Quaternion.AngleAxis(180, Vector3.up);
transform.rotation = rot1;
transform.GetChild(0).transform.localPosition = new Vector3(transform.GetChild(0).transform.localPosition.x + 100, transform.GetChild(0).transform.localPosition.y, transform.GetChild(0).transform.localPosition.z +100);
Debug.Log("case 2");
break;
case 3:
rot1 = Quaternion.AngleAxis(270, Vector3.up);
transform.rotation = rot1;
transform.GetChild(0).transform.localPosition = new Vector3(transform.GetChild(0).transform.localPosition.x, transform.GetChild(0).transform.localPosition.y, transform.GetChild(0).transform.localPosition.z+100);
Debug.Log("case 3");
break;
}
}
Note: The first child in hierarchy (child with index 0) is obviously the Terrain gameObject.
Upvotes: 0