- Home /
Rotation of Instantiated object wrong
I have a script that applies force to an Instantiated object with a direction based on the current rotation of the object the script is attached to. My problem is that the instantiated object is always rotated 90 degrees in the wrong direction.
I've tried changing the rotation of the Instantiated object, but then the applied force is is wrong.
var fwd : Vector3;
fwd = transform.rotation * (Vector3.forward);
var bulletClone = Instantiate(bulletPrefab, transform.position + transform.forward*1.7, Quaternion.identity);
bulletClone.rigidbody.AddForce(fwd * force, ForceMode.Impulse);
How do I get rotate the object without altering the direction force is applied in any way?
EDIT: Just wanted to say that the Instantiated object is a simple cylinder made in Unity. Changing the rotation of the cylinder before making it a prefab has had no effect.
Note that you can use transform.forward as a shortcut for the second line.
Answer by amunds · Apr 29, 2013 at 04:03 AM
I solved it by (perhaps) doing as you intended? Attached the script to the ship. Made a empty gameobject a child of the ship then rotated the empty gameobject 90 degrees on the x-axis. Then in my script I changed AddForce to use .up instead of .forward. Final function ended up like this:
function shootBullet() {
var bulletInstance : Rigidbody;
bulletInstance = Instantiate(bulletPrefab, spawnPoint.position, spawnPoint.rotation);
bulletInstance.velocity = spawnPoint.parent.gameObject.rigidbody.velocity;
bulletInstance.AddForce(spawnPoint.up * force, ForceMode.Impulse);
}
Answer by whydoidoit · Apr 28, 2013 at 10:36 PM
You need to put it in a parent, rotate it by 90 degrees and then make the parent the prefab.
Also fwd = transform.rotation * Vector3.forward is just transform.forward;
Thanks for the fast answer, I'm a bit confused about what you are referring to as "it", do you mean the prefab I'm going to instantiate? Currently I just have an empty GameObject prefab as the child of my Ship prefab. The script that handles the instantiate is on the empty GameObject.
I mean put your cylinder "bullet" inside an empty game object, rotate the cylinder and then make the empty go the prefab for bullet - then instantiate that prefab.
Did that now, but it had no effect. No matter how I rotate it the bullets still come out with the same rotation. $$anonymous$$akes me believe the script is setting a fixed rotation.
Well if you've done that something would have to be changing the rotation of the child of your prefab - that is pretty unlikely I would have thought... hmmm
Perhaps I'm misunderstanding something, could you please provide a simple example of the structure you'd use?
Answer by Roixo · Jan 18, 2018 at 05:27 PM
Other simple way to solve this issue without creating empty parent game object:
GameObject pc = (GameObject) Instantiate (Prefab, position, rotation, transform);
pc.transform.Rotate (new Vector3(rotationWished.x,rotationWished.y,rotationWished.z));
Where you choose in Vector3 rotationWished which rotation you want.
Your answer
Follow this Question
Related Questions
2D Rotate towards GameObject acting odd. 2 Answers
Roll a ball camera problem? 1 Answer
Rotating local transform not rotating fully if tranform.up is not exactly at (0,1,0) 1 Answer
Quaternion to Matrix Conversion Error - Camera Script 0 Answers
Rotation approximation issues using Quaternion.LookRotation 0 Answers