Rotate object with 90 degrees?
I know this has been answered like 1000 times, but I just don't know how exactly should I code. All I want is when the platform changes its position on the x or the z axis, then rotate the whole platform by 90 degrees. I tried it with a platform.transform.Rotate(0, 90, 0), so I think there is more to do. The code itself:
public GameObject platform;
public Transform lastPlatform;
Vector3 lastPosition;
Vector3 newPos;
bool stop;
private Quaternion rotationQuaternion;
void Start()
{
lastPosition = lastPlatform.position;
StartCoroutine(SpawnPlatforms());
rotationQuaternion = transform.rotation;
}
void Update()
{
}
IEnumerator SpawnPlatforms()
{
while (!stop)
{
GeneratePosition();
Instantiate(platform, newPos, rotationQuaternion);
lastPosition = newPos;
yield return new WaitForSeconds(0.1f);
}
}
void GeneratePosition()
{
newPos = lastPosition;
int rand = Random.Range(0, 2);
if (rand > 0)
{
newPos.x += 1.5f;
rotationQuaternion *= Quaternion.Euler(0, 90, 0); //one way i tried
}
else
{
newPos.z += 1.5f;
platform.transform.Rotate(0, 90, 0) //another way I tried
}
}
I appreciate all the help!
Can you explain more how you have this set up and how you intend this to work. At least give clarification to the following questions
Is the
GameObject platform
a scene GameObject or a Prefab?If it's scene GameObject then do you intent to rotate this original one or just instantiate copies with new rotation?
If it's scene GameObject then do you intent to move the original one or just instantiate copies to new position?
Sorry for not clarifying. GameObject platform is a prefab. I have 3 platforms placed in scene (because they are visible for the camera), so the third one is the 'lastPlatform'. From then, every platform is getting randomly positioned, as you can see in 'GeneratePosition()'. That works perfectly, but I want to rotate these platforms on the Y axis by 90 degrees. I edited the code in my question, so you can see what I was trying to do. I guess it's horrible.
Because the platform is a prefab, do not try rotate it.
On more question: Do you intent to give +90 degree rotation to the new platform in reference to the earlier platform, I mean like every 4th is directed same way.
Or do you want to give them all the same 90 deg rotation in reference to that original prefab?
Answer by imbruceter · Dec 08, 2021 at 07:35 PM
I managed to find a solution for the problem. Maybe could be helpful for someone, so here you go:
public struct SpawnPoint
{
public Vector3 position;
public Quaternion orientation;
public void Step(float distance)
{
if (Random.value < 0.5)
{
position.x += distance;
orientation = Quaternion.Euler(0, 90, 0);
}
else
{
position.z += distance;
orientation = Quaternion.Euler(0, 0, 0);
}
//orientation = Quaternion.Euler(0, 90, 0) * orientation;
}
}
void Start()
{
_spawn.position = lastPlatform.position;
_spawn.orientation = transform.rotation;
}
From now on, all what's left to do is Instantiate:
var newPlatform = Instantiate(platform, _spawn.position, _spawn.orientation);