[SOLVED] Random spawn works, but rotation...

Hello,

I made a random spawn script for an object in my scene.
This is the script:

public Vector3[] positions;   

    void Start ()

    {
        int randomNumber = Random.Range (0, positions.Length);
        transform.localPosition =  positions[randomNumber];
   
       
       
        if (transform.position.x > 2)
        {
            transform.rotation = new Vector3(0, -90, 0);
        }   


    }

}

And this is the component + scene pic: Screenshot - 9577ab4d22de237a90b501598ef80f95 - Gyazo

At element 3, the object also has to rotate -90 so it’s is vertical.
I tries a lot of things with eulerangles quaternion (or something like that xD) and what you see in this script (from ‘‘if’’)

(I did x > 2 because element 3 was the only one with x > 2)

I hope someone can help me with this.

Thanks

The above line is not compiling: rotation is a quaternion, not a Vector3 so it will give you this error:

Assets/MazeScript/MazeScript.cs(35,27): error CS0029: Cannot implicitly convert type UnityEngine.Vector3' to UnityEngine.Quaternion’

Check your error log, fix that error. I think you want to set it to Quaternion.Euler( 0, -90, 0);

If you don’t see errors, you might have the error checkbox OFF in the console window (upper right corner)

{
        int randomNumber = Random.Range (0, positions.Length);
        transform.localPosition =  positions[randomNumber];
   
        if (transform.position.x > 2)
        {
            transform.rotation = Quaternion.Euler( 0, -90, 0);
        } 

    }

Used this, but now my spawn object is removed / nowhere to find

Set the enemy prefab’s rotation in the inspector to what you wish, then type:

transform.rotation = Quaternion.identity;

See where that takes you. This is probably the most basic fix, let me know if it works too.

I placed it like this:

    if (transform.position.x > 2)
        {
            transform.rotation = Quaternion.Euler( 0, -90, 0);

            transform.rotation = Quaternion.identity;
        }

I don’t know if you meant to be placed there (I am really new with scripts), but it didn’t work like that. The object doesn’t disappear this time, but it also didn’t rotate

If you were not seeing the compiler error I pointed out, then you may still have other compiler errors you are not seeing. Type some garbage text into your script and make sure it gives you a compiler error, so you can be confident you are actually running the code you typed in.

Now additionally, setting the rotation to Quaternion.identity will take out all rotation and make it “normal” again, so yes, in the code example above, you won’t see any rotation. Take out the setting to identity.

Review the docs pages on transforms; they are pretty key to understanding how Unity arranges its scenes internally.

thnx for the reply.
I already looked at the transform pages, but i couldnt manage to put it in my script. But, what did you meant with ‘Take out the setting to identity’? I dont alwaar understand some english phrases. I think you are dutch? Dan kan je misschien nederlands praten

In the code above you are setting the rotation to (0,-90,0), then immediately “setting it to identity,” which is Quaternion.identity, i.e., no rotation at all (0,0,0).

That does not make sense because the previous line’s operation is completely overwritten and you will see the object is not rotated.

That is what I meant by ‘Take out the setting to identity’. Sorry for my slang! I hope that makes sense.

if (transform.position.x > 2)
        {

            transform.rotation = Quaternion.identity;
        }

This doesn’t work. Can you please just tell me where to put what in the script, because I am really confused now.

This code will rotate the object about the vertical axis. In other words it will make it spin like a merry-go-around.

This code will immediately RESET the other rotation so that the object is no longer rotated.

THIS code here, will actually rotate it along the direction you are most likely viewing it from, the Z axis:

transform.rotation = Quaternion.Euler( 0, 0, 90);
1 Like

Remove the Quaternion.Euler line and see where that takes you. Also, set the desired rotation you want your enemies to spawn, which appears to be -90 in the Y plane, in the Project tab. Just click on your enemy prefab, and set the rotation in there. Unity will read the rotation in the prefab and apply it to the enemy when he spawns :slight_smile: or at least that’s what it SHOULD do

Thank you a lot! It works great. I just set ‘‘90’’ to the wrong one. I did (0,90,0), but like you said it is (0,0,90). Really dumb of me.

Also thankyou trying to help me :slight_smile:

For the coordinates, remember XYZ and hold up your left hand

Unity is a left-handed coordinate system. Remember this by holding up your left hand in front of you like you are pushing on a door:

your thumb (the first finger) is X (straight right)
your second finger (idnex) is Y (straight up)
your palm is pushing in the Z (straight forward)

X, Y, Z, just like the alphabet!

If you use a system with right hand coordinates, it will be the same, but with your right hand.

thnx for the info