Hello!
I recently started working on a little game idea I had. In this game, I wanted to have the player character be able to lock on to their opponent and essentially “orbit” them (Think the targeting systems from Dark Souls, Zelda, or most hack-and-slash games).
Because I want to have them orbit another actor, spring arm components aren’t really going to work (as far as I’m aware, considering it would require parenting a bunch of actors together which sounds like a nightmare).
Ideally, the player should be able to move side-to-side in an orbit around the opponent, and should be able to freely move forward/backwards as well (essentially increasing or decreasing the size of the orbit).
I have something that works pretty well right now, but I’ve encountered a very weird issue that I’ve spent hours trying to debug. As the player rotates around the target actor, they spiral outwards.
This is the result of me holding right or left. The rotation works perfectly! However, the player seems to continuously move backwards. I’ve tried just about everything, and the only successful thing was a brute-force method I found of adding a target distance. However, this system made moving forward and backwards troublesome.
The way I went about doing what you see above was basically to re-purpose some of the code already available in the MoveRight function that comes with the character.
void AMyProjectCharacter::MoveRight(float Value)
{
if ( (Controller != NULL) && (Value != 0.0f) )
{
if (lockedTarget) {
orbitVector = target->GetActorLocation() - GetActorLocation();
orbitVector.Z = 0.0f;
FRotator rot(0.0f,orbitVector.Rotation().Yaw, 0.0f);
SetActorRotation(rot);
FVector Direction = FRotationMatrix(rot).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Value);
}
}
}
Anyone have any ideas as to what might be causing that spiraling effect? Honestly, it’s a pretty cool effect! If I could find a way to control it, I’d have other uses for it. However it’s really frustrating that it’s not what I want, and no matter what I seem to try and change (aside from completely getting rid of everything), nothing seems to affect it.
I believe I’ve pinpointed it down to the Distance FVector, but I’m not sure what else I could do (or what’s going wrong). I tried Flooring the values of orbitVector and rot (Someone suggested it may be a floating point rounding error in another topic), but neither of those did anything. When I tried to floor the values of Direction, I was unable to move at all.