0
\$\begingroup\$

I have a 3D tree that I want to clone so that it is perpendicular to a planet gameobject, so it looks upright when spawned. I tried by copying down all desired rotations of the tree and adding them into a dictionary full of vector3’s so that I can access any one of them when I need to. However, using Quaternion.Euler() doesn’t copy the coordinates exactly when I pass them in; they are always wrong. Below is the code:

public void PlaceFauna(GameObject prefab, Vector3 position, Vector3 rotation, GameObject icosphere) { prefab.transform.position = position; // Moves prefab into scene and sets position prefab.transform.rotation = Quaternion.EulerAngles[rotation]; // Sets rotation to that of the one in the dictionary GameObject clone = Instantiate(prefab) as GameObject; // Creates clone clone.name = clone.GetInstanceID().ToString(); // Gives clone a unique name clone.transform.parent = icosphere.transform; // Puts clone under region prefab.transform.position = new Vector3(0, 0, 40); // Moves prefab out of scene } 

It takes in the position and the rotation from the dictionary and applies .Euler() to the prefab so that the tree (should) match the position. However it does not do this. Where am I going wrong?

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

I have learnt that using Euler angles to transform a gameobject is the root of my problem as the desired position can be lost through translating between quaternions and Eulers. Since unity uses Quaternions solely and just uses Euler angles for humans to better understand, conversions can often be messed up. I worked around this problem with a different strategy; I calculated a vector between the centre of the planet and the position of the face, and set the tree’s rotation to match this vector:

prefab.transform.rotation = Quaternion(FromToRotation(new Vector3(0, 0, 1), position); 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Again, it really truly is not the translation between quaternions and Euler angles that's causing your problem. It is not true that "conversions can often be messed up" - the conversions were accurate. Your input was not correct for the desired output, leading to a common situation computer scientists call "garbage in, garbage out." We'd need to apply more troubleshooting to diagnose the source of the garbage, but it was not the conversion between angles & quaternions. \$\endgroup\$ Commented Jan 25, 2019 at 18:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.