Rotate around player but track the mouse

I have a 2d side scroller setup and I’m trying to have an object (a spawn point for pew pew pew) rotate around the player but still track the mouses movement. I honestly don’t know where to start for this one as rotation stuff just doesn’t click well for me. Anyone have an idea to solve this?

Accidentally posted this in the wrong board earlier… whoops

You mean it should rotate around something but still “look” at the mouse pointer?

You can let the object rotate around another object with, tadaaa, Transform.RotateAround!

And then just use transform.LookAt(yourmousecoordinationsin3D);

That should do the trick.

I’ve tried that before and it either doesn’t move at all or the object itself rotates in place (erratically).

It works fine for me, i attached you a little test scene so you can try it out.

345377–12053–$lookatandrotate_224.zip (1.07 MB)

OH! We had completely different ideas. Sorry if I worded incorrectly or something but I meant that the spawn points axis is the player but is always looking towards the mouse.

Like that.

You mean the canon on the image is looking at the mouse? It’s even easier and you can use one line code from my projects folder:

transform.LookAt(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(Camera.main.transform.position.z))));

Give that to the canon. The last value (Camera.main.transform.position.z) is the distance from the camera to the player. If your players position.z is one 0, just use the exact code i posted.

Hmmm… While that does make the cannon actually respond to the mouse. It seems to stop following if the mouse is almost horizontal to the player. Then as the mouse moves further away from the player the canon continues to rotate to a different angle, no longer pointing at the mouse.

You can pass a second parameter to transform.LookAt which specifies the upward direction during the rotation. If you use the axis of rotation as the upward direction, you should find the gun tracks the mouse correctly.

Awesome! I got it to work, thanks for your help guys. Sorry to have dragged this on for so long.