- Home /
How to rotate object in instantiate with float value
I'm trying to instantiate an object, and rotate it based off a float value. This is what I tried: public GameObject toInstantiate; //Drag in the object to instantiate here float modifierF = 10.0f public GameObject instantiatePoint; //Drag in the empty game object here
void Function () {
Instantiate(toInstantiate, instantiatePoint.transform.position, instantiatePoint.transform.rotation.x + modifierF, instantiatePoint.transform.rotation.y, instantiatePoint.transform.rotation.z);
}
I've even tried using a vector 3 in place of it. I tried Euler Angles. I want the instance to NOT be a child, spawn at the rotation of the reference point (instantiatePoint) and then change specific x, or y, or z angles based off a float value.
I've tried storing the created instance in a GameObject variable, however the object won't be rotated how I want it to.
What happens is that it simply sets its rotation to (0,0,0) based on world axis, not the local rotation of the reference point.
Answer by Nighfox · Jan 26, 2018 at 03:53 PM
Instantiate(spawnObject, spawnPoint.transform.position, Quaternion.Euler(spawnPoint.transform.rotation.eulerAngles.x + modifierX, spawnPoint.transform.rotation.eulerAngles.y + modifierY, spawnPoint.transform.rotation.eulerAngles.z + modifierZ), null);
Use transform.rotation.eulerAngles
, not just transform.rotation
. Also use Quaternion.Euler
instead of EulerAngles
as the latter is already deprecated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why is my projectile firing "off" after rotation 1 Answer
Creating objects which are facing center of a sphere 1 Answer
Limit Horizontal Rotation 1 Answer