- Home /
How can I change rotation of spawning platforms?(platforms must appear at angles of -45 degrees Y)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawnplatforms : MonoBehaviour
{
public GameObject platform;
private GameObject platforminst;
private Vector3 platformpos;
private float speed = 5f;
void Start()
{
platformpos = new Vector3 (Random.Range(0.93f, 1.74f), Random.Range (2.18f,-3.36f),0f);
platforminst = Instantiate(platform, new Vector3(5f, -6f, 0f), Quaternion.identity) as GameObject;
platforminst.transform.localScale = new Vector3(Random.Range(1.2f, 2f) , platforminst.transform.localScale.y, platforminst.transform.localScale.z);
platform.transform.localRotation = //*what I must write ?*
}
void Update()
{
if (platforminst.transform.position != platformpos)
platforminst.transform.position = Vector3.MoveTowards(platforminst.transform.position, platformpos, Time.deltaTime * speed);
}
}
Answer by GreyMiller · Dec 30, 2019 at 01:32 PM
Try using these:
a) transform.localRotation or transform.rotation like you use transform.position.
b) Quaternion.Euler or Quaternion.Slerp (I'm not quite sure about these two though, you better read about them in API)
The following might help:
https://docs.unity3d.com/ScriptReference/Quaternion.html
https://docs.unity3d.com/ScriptReference/Transform-localRotation.html
I haven't learned much about quaternions in Unity, and I have no time to test all your code, but as I see it: your platform.transform.localRotation should have type of quaternion, so try:
platform.transform.localRotation = Quaternion.Euler(0, 45, 0);
However, ins$$anonymous$$d of this (check first if this works) I would try
platfor$$anonymous$$st = Instantiate(platform, new Vector3(5f, -6f, 0f), Quaternion.Euler(0, 45, 0)) as GameObject;
And read more in scripting API, all I advised here I have just learned from there. Example codes there are very useful. This has a different example:
https://docs.unity3d.com/ScriptReference/Transform-Rotation.html
TAN$$anonymous$$ YOU VERY $$anonymous$$ATCH!!!