Hello,
I’ve seen this question a few times on this forum but apparently to no avail, I’m missing something simple that I cannot figure out: I would like to instantiate a laser beam going from the player position to the enemy position. Simple as that:
In 2.5D the beam shoots into the Z axis when the player is to the right of the enemy and towards the camera when the player is to the left of the enemy. Drawing a line (just to double check) between the player and enemy position works. Instantiating the laser beam with LookRotation, doesn’t.
Is the laser beam in your prefab aligned with the prefab’s forward (Z) axis?
Is the laser a child of some other game object whose rotation might be affecting it?
If you pause the game after creating the laser and examine the new prefab, is there anything notable about its rotation? Is any axis aligned with your desired direction?
Hi @Antistone and thanks for coming to the rescue!
Yes the beam and prefab’s forward axis are the same (X in my case as I’m in 2.5D)
No it’s just by itself
This is what happens when I pause the game:
Red cube: player
Gray cube: enemy
Green line: the line drawn from player to enemy
Beam: the Beam that goes wherever it likes.
This is the player’s transform (red cube):
This is the enemy transform (gray cube):
And this is the laser beam instantiated with Quaternion.LookRotation():
The beam appears to have the Z axis as the up vector (this is the result of LookRotation() if I instantiate the prefab with Quaternion.Identity() the up vector is Y and forward vector is X).
I’m pretty confused. The screenshot you say is the transform values of your laser shows all zeroes for the rotation, but the screenshot below it where you appear to have the beam selected is clearly showing the axes pointing in directions other than horizontal and vertical, so its rotation value can’t be all zeroes.
Also, it looks like the blue arrow (Z axis) is pointing towards the enemy in your last pictrue. So I suspect if you change your prefab so that it points along the Z axis, that will likely solve your issue. (The Quaternion.LookRotation function does not know or care that your game is supposed to be 2.5D; it’s just doing math on the vector you give it.)
Alternately, if you want the beam’s X axis to point towards the enemy, you can probably make that happen using some different math. You could probably adjust your current rotation by multiplying by something like Quaternion.AngleAxis(-90, Vector3.up).
I managed to fix it, should it be useful for posterity: the laser beam is made of 3 different components, 2 of them where rotated by 90 degrees on the Y axis. As suggested by Antistone, I reset the rotation on all prefab’s components so that they all point forward (along the Z axis) and now the beam works as it’s supposed to.