I am trying to instantiate 10 GameObject, and for doing so, I have an array that stores their position and rotation. The MazeStart() function randomly selects the coordinates and rotation from the array and calls the SpawnATM() function to instantiate the GameObjects. The SpawnATM() creates a Vector3 and a Quaternion, using the values from the array before instantiating the GameObject.
The problem is that, despite the Quaternion taking the values I want, while running the game, the rotation of all the GameObjects is set to 180, independently of the value inside the Quaternion.
When I use a specific value instead of MazePos[ATM,3], it rotates the GameObjects to that value.
I have the following code:
public class Maze1 : MonoBehaviour{
public GameObject ATMPrmoufa;
// MazePos[ No. , x/y/z....] v-int-v
// x , y , z , RotationY
public float[,] MazePos = { { -237.4f , -4.625f , -52.60674f , 180},
{ -187.1933f , -4.625f , -66.2f , 90},
{ -167.6068f , -4.625f , -50.4f , -90},
{ -141.4f , -4.625f , -46.60674f , 180},
{ -137.6068f , -4.625f , -32.4f , -90},
{ -203.7f , -4.625f , -46.706f , 180},
{ -203.7067f , -4.625f , -35.4f , -90},
{ -187.1933f , -4.625f , -29.4f , 90},
{ -215.7067f , -4.625f , -20.4f , -90},
{ -160.1933f , -4.625f , -17.4f , 90}, };
int ATM;
public GameObject[] ATMSPR= {null,null,null};
public bool FirstRun = true;
public void MazeStart() {
List<int> numbers = new List<int>();
for (int i = 0; i <= 9; i++) {
numbers.Add(i);
}
System.Random rand = new System.Random();
for (int i = 0; i < 3; i++) {
ATM = numbers[rand.Next(numbers.Count)];
numbers.Remove(ATM);
SpawnATM(ATM, ATMSPR[i]);
}
for (int i = 0; i < 7; i++) {
ATM = numbers[rand.Next(numbers.Count)];
numbers.Remove(ATM);
SpawnATM(ATM, ATMPrmoufa);
}
}
public void SpawnATM(int ATM, GameObject pr,) {
UnityEngine.Vector3 ATMPos = new UnityEngine.Vector3(MazePos[ATM,0], MazePos[ATM,1], MazePos[ATM,2]);
UnityEngine.Quaternion CoinQ = new UnityEngine.Quaternion(0,MazePos[ATM,3],0,0);
Instantiate(pr, ATMPos, CoinQ);
}
I have also checked out other questions with similar problems but haven't found anything fitting to my case
I am trying to instantiate 10 GameObject, and for doing so, I have an array that stores their position and rotation. The MazeStart() function randomly selects the coordinates and rotation from the array and calls the SpawnATM() function to instantiate the GameObjects. The SpawnATM() creates a Vector3 and a Quaternion, using the values from the array before instantiating the GameObject.
The problem is that, despite the Quaternion taking the values I want, while running the game, the rotation of all the GameObjects is set to 180, independently of the value inside the Quaternion.
When I use a specific value instead of MazePos[ATM,3], it rotates the GameObjects to that value.
I have the following code:
public class Maze1 : MonoBehaviour{
public GameObject ATMPrmoufa;
// MazePos[ No. , x/y/z....] v-int-v
// x , y , z , RotationY
public float[,] MazePos = { { -237.4f , -4.625f , -52.60674f , 180},
{ -187.1933f , -4.625f , -66.2f , 90},
{ -167.6068f , -4.625f , -50.4f , -90},
{ -141.4f , -4.625f , -46.60674f , 180},
{ -137.6068f , -4.625f , -32.4f , -90},
{ -203.7f , -4.625f , -46.706f , 180},
{ -203.7067f , -4.625f , -35.4f , -90},
{ -187.1933f , -4.625f , -29.4f , 90},
{ -215.7067f , -4.625f , -20.4f , -90},
{ -160.1933f , -4.625f , -17.4f , 90}, };
int ATM;
public GameObject[] ATMSPR= {null,null,null};
public bool FirstRun = true;
public void MazeStart() {
List<int> numbers = new List<int>();
for (int i = 0; i <= 9; i++) {
numbers.Add(i);
}
System.Random rand = new System.Random();
for (int i = 0; i < 3; i++) {
ATM = numbers[rand.Next(numbers.Count)];
numbers.Remove(ATM);
SpawnATM(ATM, ATMSPR[i]);
}
for (int i = 0; i < 7; i++) {
ATM = numbers[rand.Next(numbers.Count)];
numbers.Remove(ATM);
SpawnATM(ATM, ATMPrmoufa);
}
}
public void SpawnATM(int ATM, GameObject pr,) {
UnityEngine.Vector3 ATMPos = new UnityEngine.Vector3(MazePos[ATM,0], MazePos[ATM,1], MazePos[ATM,2]);
UnityEngine.Quaternion CoinQ = new UnityEngine.Quaternion(0,MazePos[ATM,3],0,0);
Instantiate(pr, ATMPos, CoinQ);
}
I have also checked out other questions with similar problems but haven't found anything fitting to my case
Share Improve this question edited Mar 18 at 8:31 DarkBee 15.5k8 gold badges72 silver badges117 bronze badges asked Mar 15 at 18:40 FKsoFKso 356 bronze badges2 Answers
Reset to default 2A Quaternion
you can't just create via the constructor using an Euler space angle. The names for x, y, z, w
may a bit misleading and could as well be called a,b,c,d
. (They are just aligned with Vector4
which in return is aligned with Vector3
and Vector2
.) But these x,y,z,w
are not linked to the axis X,Y,Z but rather a matrix system of complex numbers and range from -1
to 1
.
Instead you simply want to use Euler
in general
UnityEngine.Quaternion CoinQ = UnityEngine.Quaternion.Euler(0, MazePos[ATM,3], 0);
and in your specific case maybe even AngleAxis
UnityEngine.Quaternion CoinQ = UnityEngine.Quaternion.AngleAxis(MazePos[ATM,3], Vector3.up);
which internally indeed will do something similar to this answer (but why reimplement things ;) )
Yeah, you're quaternion initialisation is a bit off _(right idea, wrong units)_
Firstly, your rotations are specified in degrees. You will need to convert those to radians.
Radians = MazePos[ATM,3] * (PI / 180.0);
The quaternion uses half angles, not full angles, e.g.
HalfAngle = Radians * 0.5;
Whilst the first 3 values are the axis of rotation (which in your case is [0,1,0]), which has been scaled by sin(HalfAngle)
. The last value is always cos(HalfAngle)
. So you should be using something like this:
UnityEngine.Quaternion(0, sin(0.5 * MazePos[ATM,3] * (PI/180)) ,0, cos(0.5 * MazePos[ATM,3] * (PI/180)))