How can I rotate an object so it always rotates towards a first person camera?

I’m working on a project where the player can lift an object in front of them, once they hold an item they can rotate it with the mouse input while holding a button.

I’ve manage to make it so that moving the mouse from side to side rotates the object on the Z axis, but I want to make it so that moving the mouse up and down always tilts the object towards and away from the camera regardless of which way the camera is pointing.

If I rotate it on the X or Y axis in local space it just tilts it depending on where the front of the object is pointing. Rotating it in world space tilts it the same direction regardless of everything else.

I think I either need to use the Rotator from Axis and Angle node, or figure out how to calculate the right amount of X and Y rotation to use in world space. But I can’t wrap my head around it.

I think an easy solution would be to add any component (maybe an Arrow component) or a socket that would act as an “Inspect Item Handle” as a child of your first person camera component and move it to the location you want the held object to be away from the camera.

Then when you want to inspect an object, attach it to that component and any rotation applied to the object will be always relative to the camera and that would do what you want.

Then take control of your Turn or Lookup input events if an object is held (simple boolean check or check if the component has any children) and apply relative rotation based on the mouse axis value. Here, i set up something quick for ya, hope it helps.

Thank you very much for your answer, but I don’t think I can solve it that easily without starting over. I’ve already set up a system that smoothly moves the object to the right place without attaching to components. I tried combining your solution with my system, but I don’t think I can do it without losing my smooth movement, and some other little things.

What I’ve been trying to do is set up a way to dynamically calculate a relative rotation that is angled in the right direction, but that seems to be more difficult than I first thought.

Oh i understand, can you try this instead then? it should work :smiley:

and use camera’s forward vector for the left and right rotation.

2 Likes

Thank you, I managed to fix it using this :slight_smile: