- Home /
rotating the object in the correct orientation
Hi im wondering how to get my objects rotation right
I was using my preview objects rotation but if were to rotate it I would have to use quaternion euler
but when i use it this happens: link
the object is rotated in the wrong orientation no matter what I do
My script public void ChangeCurrentsBuilding()
{
//GameObject curprev = Instantiate(currentobject.preview, currentpos, currentobject.preview.transform.rotation) as GameObject;
GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentRot)) as GameObject;
currentpreview = curprev.transform;
}
public void startPreview()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, 50, layer)) {
if (hit.transform != this.transform) {
showPreview(hit);
}
}
}
public void showPreview(RaycastHit hit2) {
currentpos = hit2.point;
currentpos -= Vector3.one * offset;
currentpos /= gridSize;
currentpos = new Vector3(Mathf.Round(currentpos.x), Mathf.Round(currentpos.y), Mathf.Round(currentpos.z));
currentpos *= gridSize;
currentpos += Vector3.one * offset;
currentpreview.position = currentpos;
if (Input.GetButtonDown("Fire2")) {
currentRot += new Vector3(0, 90, 0);
currentpreview.localEulerAngles = currentRot;
}
}
public void Build()
{
PreviewObject PO = currentpreview.GetComponent<PreviewObject>();
if(PO.isBuildable)
{
Instantiate(currentobject.prefab, currentpos, Quaternion.Euler(currentRot));
}
}
}
[System.Serializable]
public class buildObjects {
public string name;
public GameObject preview;
public GameObject prefab;
public int gold;
}
Answer by Glurth · Feb 25, 2018 at 06:53 PM
currentpreview.localEulerAngles = currentRot;
This line will affect the rotation of the object, but only relative to it's parent. If the parent-object is rotated about say... the X axis, then adjusting the child's localRotation about the Y axis, will NOT change the object's rotation around the X axis. However, if you were to modify the object's world-space "rotation"
currentpreview.rotation = Quaternion.Euler(currentRot);
This will set the orientation relative to the WORLD (as opposed to relative to the parent).
I mention this because in the images it looks like the "correct" version is rotated about the X (or Z) axis, but the "incorrect" one is not. So, you either need a parent object that is roatated about X, OR, you need to specify that rotation in your initial Euler angle (initial, meaning: don't add it again everytime you rotate!).
so how do i specify the rotation in my initial euler angle or how do i parent the preview object
"how do i specify the rotation in my initial euler angle" You can do this in the when you instantiate the object, simply adjust the quaternion passed to the Instantiate Function:
GameObject curprev = Instantiate(currentobject.preview, currentpos,Quaternion.Euler(90,0,0)) as GameObject;
currentpreview = curprev.transform;
To Parent the object: Create an empty-gameobject before creating your object, then pass that empty-gameobject to the instantiate function:
GameObject parent= new GameObject();
parent.transform.rotation=Quaternion.Euler(90,0,0);
GameObject curprev = Instantiate(currentobject.preview, currentpos, Quaternion.Euler(currentRot), parent) as GameObject;
It works but when i rotate it doesnt rotate on the correct axis tho
Your answer
Follow this Question
Related Questions
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
Rotation issue with remote players projectiles 0 Answers
How to check for NaN 4 Answers
My Raycast dont hit. 3 Answers
How to mirror limb movement 0 Answers