# Calculate rotation to set on static mesh component to face location in world space?

I’m trying to have a static mesh component of an actor face towards a location in world space.

The function I have so far works fine if the actor is on a flat horizontal surface, but when on a slope (so it’s Pitch and Yaw are != 0), the rotator doesn’t point accurately. Code:

``````simulated function PointMeshCompAtWorldLoc(vector worldLoc)
{
local rotator faceToLoc;

faceToLoc = Rotator(worldLoc - Location) - Rotation;

MyMeshComp.SetRotation(faceToLoc);
}``````

Anyone know what I’m missing here? Thanks in advance.

Would it be good enough if instead of using the whole rotation, you just used the yaw?

``````simulated function PointMeshCompAtWorldLoc(vector worldLoc)
{
local rotator faceToLoc;
local rotator YawOnly;

YawOnly.yaw = Rotation.yaw;

faceToLoc = Rotator(worldLoc - Location) - YawOnly;

MyMeshComp.SetRotation(faceToLoc);
}
``````

My only other suggestion would be to use quaternions wherever possible.

I’ve read several other places that mention using quaternions for similar tasks.

I’m honestly not really sure where to start. Guess i need to turn to ChatGTP to get some pointers…

If you have any tips/examples, they would be welcome.
Cheers

Open up Object.uc and do a search for “Quaternion functions” and you’ll see a comment with a bunch of functions under it. That’s your toolbox. I think you’ll want to do this:

``````simulated function PointMeshCompAtWorldLoc(vector worldLoc)
{
local rotator faceToLoc;
local Quat QuatToTarget;

QuatToTarget = QuatFindBetween(Location, worldLoc);
faceToLoc = QuatToRotator(QuatToTarget);
faceToLoc -= Rotation;
MyMeshComp.SetRotation(faceToLoc);
}
``````

Thanks for the suggestion. I wasn’t able to get it to work correctly though. I ended up deciding to keep the actor only rotating it’s yaw, so that i could continue using my original function. And then just rotate other smc’s it has to simulate the actor rotating.

I’d like to revisit it at a later date to implement the proper solution, but for now I’ll have to leave the hack solution in place, which works fine. Frustrating to not solve it though…

This simple function works for me.

function FindAimToHit(Vector Origin, out Vector AimSpot, out Rotator AimRotation)
{
AimRotation = Rotator(AimSpot - Origin);
}