1

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: Added a couple of images to better portrait the problem. Empty gameObj with terrain and objects as children with pivot centered

Showcase of terrain unintended rotation when applying 90deg to empty gameobj

Weedosaurus
  • 140
  • 8
  • Some pictures would really help here. – Iggy Jun 18 '20 at 01:34
  • I'm sorry, I didn't realize someone had answered this. I'm currently at my phone, I will upload some pics when I get the chance. Thank you. – Weedosaurus Jun 22 '20 at 21:26
  • Done adding the pictures. Waitting for reply. Thank you in advance. – Weedosaurus Jun 25 '20 at 14:20
  • I feel like you should be able to parent it to an empty game object. Have you tried toggling the pivot mode (it's in the top left of the screen, after the move/rotate/scale controls). – Iggy Jun 25 '20 at 16:33
  • Yes and yes. I've tried both and the behaviour is the same. It still rotates around the pivot on the corner and not on the center. – Weedosaurus Jun 27 '20 at 17:54

1 Answers1

0

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.

Weedosaurus
  • 140
  • 8