- Home /
How to rotate an Instantiated object?
Hello i need help rotating lasers becuase when my player rotates, the laser does not face the direction the player is shooting. Here's some of my script right now -
var ourRotation : int = transform.rotation.y;
if(Input.GetButtonDown("Jump")) {
//create our laser
var laser0 = Instantiate(laserPrefab, transform.Find("spawn").transform.position,Quaternion.identity);
//add force to our laser
laser0.rigidbody.AddForce( transform.forward * force);
//rotate our laser
laser0.Rotate(0, ourRotation, 0);
}
Can you put code tags around it? And sorry, I can't help to much... Check the API, they have some stuff on it there...
Answer by Helix · Dec 13, 2010 at 03:14 AM
Hullo Game hound,
It's hard to tell exactly how your laser prefabs work, but looking at your code, there are a couple of things you could try changing. For starters, I'd make var ourRotation
a float
instead of an int
, for the sake of precision. I don't see why you'd even need to use an extra variable for rotation in this case, the line
var laser0 = Instantiate(
laserPrefab, transform.Find("spawn").transform.position,Quaternion.identity);
could be re-written as
var laser0 = Instantiate(
laserPrefab, transform.Find("spawn").transform.position,transform.rotation);
Possibly, the reason your code didn't work, was that laser0.Rotate
should be laser0.transform.Rotate
. I don't think that should even compile without error. Again, I don't know how your lasers work exactly, but you can prolly leave that bit out now, making the final result...
if(Input.GetButtonDown("Jump")) {
var laser0 : GameObject = Instantiate(laserPrefab, transform.Find("spawn").transform.position,transform.rotation);
laser0.rigidbody.AddForce( transform.forward * force);
}
Hope this helps!
Well it didn't work, but I should tell you how the lasers work - They're just only a simple cube with a scale of (0.5, 0.5, 1), it's kind-of my first game, and it's a prefab as you can tell from the var's name. Also look at this scenario - A player shoots when he's at 0 degrees ,y, and it looks good, but when he's not at 0 or 180 degrees ,y, the laser still is still spawning a (0,0,0) rotation.
So its moving in the right direction but not facing the right way? Wierd. Is the laser prefab running a script that could be resetting the rotation? You could add in laser0.transform.Rotate() and try putting in any values at all to test if this affects the laser's rotation, or if there is something else to blame.
I went to my laser's script (wich is a "OnCollisionEnter" script) and added
function Start () { transform.Rotate(0, gameObjeect.Find("SpaceShip").transform.rotation.y,0); }
. . .I also got rid of the rotate code in the script that I showed and still didn't work maybe there is something else blame, but I'll try different rotation codes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090813im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to rotate an instantiated object by a fixed amount before it is spawned 2 Answers
Upon being instantiated, why is my object moving upwards ? 2 Answers
Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers
Instantiated object rotation abruptly. 0 Answers
Instantiate question 1 Answer