- Home /
Instantiating object with transform.rotation not working?
if (Vector2.Angle (transform.forward, other.transform.position - transform.position) < 1) {
_shotTimer += 1 * Time.deltaTime;
if(_shotTimer > 0.2)
{
if (_whichWeapon) {
_laserobj = Instantiate (_laser, _position.transform.position, _position.transform.rotation) as GameObject;
_shotTimer = 0;
_whichWeapon = false;
} else {
_laserobj = Instantiate (_laser, _position2.transform.position, _position2.transform.rotation) as GameObject;
_shotTimer = 0;
_whichWeapon = true;
}
}
} else {
_shotTimer = 0;
}
So, i have a little spaceship, really small, scale factor 0.003... What i want to happen is, when it shoots a laser, the laser has the same rotation as the objects referenced in the variables (_position and _position2)... The thing is..., unless the ship is facing 0,0,0 rotation, the laser is instantiated with and odd rotation... This happens:
Is any thing wrong with the instantiation code?
What are position and position2 in that code?
I'll just assume they are gameobjects that are children of your fighter object that serve as launch positions for your laser object, because that would make sense.
In which case, your instantiation code looks fine afaict. $$anonymous$$aybe the problem is in the laser object? How does it move?
Ahah, i just found the problem xD! Befour i explain my self, i should thank you and possibly future anwers :D!
The problem was in the script that managed the laser movement, i had this to move:
transform.translate (transform.forward nº time.deltaTime);
and i forgot to put the "space.World" after that, wich caused the laser to move the wrong direction... the initial direction it was instantiated was right :D!
Again thank you ;)!
How do i close this question? if it has that option xD?
Dang! I also forgot to say that those two variables "position" and "position2" are indeed the positions for the instantiated lasers xD
Your answer
![](https://koobas.hobune.stream/wayback/20220613190810im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Physics related rotation problem. (a weird one) 0 Answers
Freeze object rotation on Polybrush 1 Answer
Instantiated Object looses prefab's rotation 10 Answers
Can't reference Camera in Prefab 1 Answer