constantly rotating a moving object towards the mouse cursor

I have a vehicle with a turret. I want my turret to look at the mouse cursor while moving with the vehicle. I used find look at rotation for this, but when I start the game it works very well when I don’t move, but if I move the vehicle and start moving the turret at the same time, the turret starts to deviate from the cursor. I set the sphere object that appears in the codes to the mouse cursor

Here is the problem and the blueprint nodes I use



You probably get some vectors wrong. Use line trace and turn on debug draw, First line trace from turret to where cursor is (location to location), then second draw in direction to mouse cursor but using look at rotation (make forward vector of turret, multiply it by 1000-2000, rotate by look at rotation, and draw from turret location to turret location plus that vector).

Can you share an image? I’m new to unreal engine.

I do not have code for tank game, however i have some code for my inertial space ship project:

It rotates whole mesh, not component.

Check if you have all locations for tank, turret and mouse as WORLD locations. If you mix local and world everything will work as long as you do not move tank from 0,0,0. When you move if away world location will be different than local location.

I tried to find the rotations of the two objects by setting them in the world location as you said, but the problem continued as in the video.

In your blueprint and vid:

  • i suspect problem is in calculating real world rotation, then applying it as local component/socket rotation
  • New Var that is transferred to Turret Control is world space Yaw rotation
  • i am not sure if “Component to Local” will translate that rotation from world to local.

I kind of see that tank rotation is added to look at rotation (not quite sure from video).
Not sure if this will work (no time to test it):

  • get rotation of whole mesh (aka base of tank)
  • combine that rotation “with look at rotation” (you may need to negate base Yaw rotation first.)
  • use combined Yaw
  • as long as all this is on flat (or almost horizontal plane) you can just add (or substract) base Yaw from look at Yaw.
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.