Hi, I’m trying to click and drag with the mouse, and at Input.GetMouseButtonDown(0), I want a plane (positionPlane) to be positioned at that point (PressLMB), and then when I release the mouse button (ReleaseLMB), the Z-rotation of the plane should be set with the angle between PressLMB and ReleaseLMB.
The positionPlane is attached to Main Camera.
When I start the game and look in the same direction as the positive global Z is pointing, then it works, but in all other directions, the plane will be set to the wrong angle most of the time.
Can you perhaps draw a sketch of what you want to achieve? An angle between two points isn’t defined, so you probably want an angle between those two points relatively to a third point.
i also note that you are using Vector3.forward, which is always 0,0,1 - perhaps you meant transform.forward?
The positionPlane.transform.position gets X,Y,Z from PressLMB
The positionPlane.transform.rotation Z is changed to Angle
This seems to be working when I’m looking in the same direction as the global Z-axis, which makes me wonder if the plane or code is sentitive to the world coordinate system, but the plane has been placed in Main camera…
You are calculating the angle based on the X and Y values of the mouse positions in world coordinates and then using that to set an angle in local coordinates. If the camera were (say) looking down the X axis, then the X coordinates of the clicks might be effectively the same everywhere on the entire screen.
Try calculating “direction” using the screen coordinates of the mouse down+up directly, rather than converting them to world coordinates first.
Good point, that is what is happening. I have tried to fix that, but since the camera follows the mouse, the screen value of the mouse will always be at the center of the screen. I’m not sure how to solve that with the current code, but I think I will need to get world coordinates and relate to these, in order to get a delta value…
I searched around for functions to use, and rewrote the code to the one below. It makes the plane always look at startPos regardless of which direction I look at which is great, but it makes the plane rotate around its X, Y and Z axes.
Can I get the plane to only try to look at startPos by rotating around its X-axis? I can’t find how I can only rotate around one axis.
I have to admit I am constantly confused about what axis you want to rotate around and how your plane is lying in its local space. In your initial post you wanted to rotate around z, now around x. But perhaps that is a change prompted by our suggestions. I think I was also surprised by your pictures because I assumed (without reason) that the plane would be facing the target point instead of going through it as seems to be the case in your pictures.
I’ll dare a guess and say that perhaps you want your plane to always be vertical. If that is the case you could lift the point you’re looking at to the height of the plane’s location and things should work out. Essentially, you should get a rotation around the y-axis.
If the plane is not supposed to have a special orientation with respect to the world (i.e. vertical or horizontal) you can still use a similar approach but would have to convert the world point to local coordinates temporarily.
This example rotates around the y-axis, but you can change it to a different axis by setting the correct local value to 0 and using the corresponding axis as second parameter in LookAt.
Great, you both hint at the same idea i think, and that second code works for me! Thanks alot!
Ah, yes, I changed the axis from Z to X since I found the LookAt function and used it at the second attempt, which uses the Z-axis by default, so I tried to lock the X-axis instead. Also the initial images are inaccurate I think; the plane should have moved to ReleaseLMB position, since the plane follows the camera and mouse position.
I can’t believe you managed to still give me the correct code
I’ve just begun trying to learn Unity, so I’m mostly throwing code againt the wall to see what happens, while trying to create something. But this was a good lesson on some of the fundamental aspects at the same time. Thanks again!