- Home /
laserbeam on a 90 degree angle
Hi, Im new to Unity and scripting. My problem is my spaceship which shoots the laser beam with an 90 degree angle. This is the code.
GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation);
newBullet.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * bulletForce);
Destroy(newBullet, 2.0f);
currentAmmoState--;
There is no cote on the laser beam itself. Best Regards.
Welp, I'm also not that good with Unity2D, but I'll try to help: I think the problem is with the "GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation);". You're saying "spawn" a laser with the same rotation as the ship? (If the script is attached to the ship". $$anonymous$$aybe, the sprite of the ship doesn't match the ship's gameObject's rotation. I mean, your code seems right to me, but maybe the "front" of the ship sprite is wrong. If the laser is a sprite, and not a line renderer, maybe the sprite of the laser is wrong, not the ship. Is the sprite of the ship in the middle of the pivot? and so on. I can try to help, but I would need to see the "blue, green, red arrows of the gameObjects". (I'm kinda dumb, I can't really tell what's going on in the picture ;( )
Answer by Captain_Pineapple · Jul 06, 2020 at 05:40 PM
Basically what @Miaw666 said was correct. The "bullet" you instantiate is just not oriented correctly.
simply rotate it by 90 degrees by using newBullet.transform.Rotate(90f, 0f,0f);
The angle might need a - and/or you might have to set the "90" in a different axis of the rotation function as i don't know which axis you have to rotate about. That detail is missing in the post. Be aware here that this will also change the Vector you have to use for the force you apply so the Vector2.up
will probably have to be something like Vector2.forward
.
Answer by Pro-Ton · Jul 06, 2020 at 07:27 PM
Just an addition to @Captain_Pineapple 's answer, if your game is 2D, you need to rotate it by 90 degrees on the z-axis, and in that case, Vector2.up
will work fine. Another way to rotate this is by changing the 1st line to
GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation * Quaternion.Eular(0,0,90)); // to rotate it 90 degree more from the initial rotation
Concatenating Quaternions is done using noncommutative multiplication.
In other words, you apply each in turn by multiplying them, and you get a different result depending on which comes first in the series.
Quaternion a, b;
(a * b) != (b * a);
The reason the order matters can be described with an example of two 90-degree rotations (provided based on thumb/index finger as models):
Example: rotate 90 degrees right, then 90 degrees forward
1: forward is now right, up is now up
2: forward is now right, up is now forward
Example: rotate 90 degrees forward, then 90 degrees right
1: forward is now down, up is now forward
2: forward is now down, up is now right
Holy shirt... I didn't expect it to be this complex. But I get what you mean, thanks for the lesson :)
Your answer
Follow this Question
Related Questions
Line Renderer & Raycasting SOS !!! 1 Answer
why can't Line Renderer catch movement of camera? 0 Answers
GVR laser pointer maximum distance not changing 0 Answers
coroutine(Firelaser)couldn't be started 2 Answers
How to make a laser particle? 0 Answers