- Home /
Instantiate a GameObject with a specific Z rotation
Hi there ! First, sorry for the english mistakes I'm not from England or wathever. Here is my problem : I'm making a demake (NES style) of Portal from Valve and my problem is when I create a portal. When I'm instantiate it, the portal will appear in his origin rotation. I tried to impose a specific Z rotation to the portal but it will always appear with the same Z rotation. What I want is when the portal appears on a wall the portal is vertical and when the portal appears on a ceiling it is horizontal. I saw on Internet to use Quaternion but I don't understand it... I put some screenshots of the game and the portal's script for you to understand my problem better. Thanks to the poeple who will help me :)
Game view > https://ibb.co/6Wdf6xY Script > https://ibb.co/D8x2pj7
Answer by mehamednews · Jul 06, 2019 at 02:10 AM
I suggest using a raycast to check if the surface is a WALL or CEILING, Then Change the rotation of the portal according to that
To set a specific rotation (while instantiating the portal) use "Instantiate" And to check the type of surface use "raycasting"
Yeap, check the normal of the surface behind portal and make portal's transform.forward = normal. Ofc make sure your portal's forward direction is the right one, it should be z positive i guess.
Thanks @mehamednews. I tried collisions tags with raycasts and it works ! But I didn't understand everything about raycasts and I hate when I don't understand my script ahah. So what I did is if the bullet (blue or orange) hits a wall : Instantiate(blueBullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))); and if it hits a ceiling : Instantiate(blueBulletl, transform.position, Quaternion.Euler(new Vector3(0, 0, -90))); The bulletes have rigidbody2D attached to. $$anonymous$$yabe it's not the best way to do that but I understand better compared to raycats. But thanks for your help it helped me anyway and I hope it will help other people :)
Answer by Marioooo · Jul 06, 2019 at 06:48 AM
you can use qaternion.AngleAxis its a quaterion unit, but you can set it as a axis rotation, easy to understand https://docs.unity3d.com/ScriptReference/Quaternion.AngleAxis.html on the quaterion section, you can declarate a qaternion.angleaxis and set the z value