Download

Can't figure out how to rotate something on its local axis

Hello! I just can’t seem to wrap my head around how you should rotate something locally. I’ve been trying to make it work for a few days now but I just don’t get it.

I have this weapon code here where I’m spawning the projectiles. What I’m trying to do is roll (rotate its x axis) it (the projectile) a certain amount based on how many pellets the weapon unleashes in a blast, then pitch (y axis) it up a bit so that I get a circle where the pellets can go and somewhat guarantee that the random calculations won’t make them all go in the same direction. This is for a shotgun specifically.

Thank you in advance!

If you just want a random circle pattern you could try something similar to this


    const FVector randomPelletDirection = FMath::VRandCone(aimDirection, FMath::DegreesToRadians(ConeHalfAngleInDegree));
    return FRotationMatrix::MakeFromX(randomDirection).Rotator();

ConeHalfAngleInDegree could be considered the max pellet deviation in degree.

Thanks for your reply, Garner.

Let me clarify what I meant: I don’t know which funtion to use to rotate it. Would it be projTran.SetRotation()?
Also, I don’t want a completely random pattern since that can (although pretty unlikely) cause them all to land at the same spot.

projTran.Rotation = FRotator(…).Quaternion(), or you can set it as a FRotator in the contructor for FTransform
projTran.SetRotation(FRotator(…).Quaternion());

Thank you, Telimaktar. How do you do a local rotation, though? Because I want to roll it and then pitch it up on its own Y axis so that they spread out, currently they only spread straight upward in a line.