Download

Twin Stick Rotation Problem

I’ve got my left stick on the gamepad set up to move the character in the direction pushed, based off the direction of the camera.
This works great, and I wanted to do pretty much the same thing for the right stick, except I’d simply make the character rotate on the spot to face the direction pushed on the right stick. However, it seems to snap to the 4 directions (up, down, left and right). When I draw the debug line it only snaps to the 4 directions.

I’m not sure why because it’s pretty much the same code I’ve used on the left stick. I’ve debugged the values and it seems to be correct, as in it goes from -1 to 1 when pushing from down to up on the right stick.

Here is the code from the up/down Left stick stuff:


void ATutorialCodeCharacter::MoveForward(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		// find out which way is forward
		const FRotator CameraRotation = FollowCamera->GetComponentRotation();
		const FRotator YawRotation(0, CameraRotation.Yaw, 0);

		// get forward vector
		const FVector CameraFacingDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(CameraFacingDirection, Value);

		bMoving = true;
		LeftStickDirection.Y = Value;
	}

	if (Value == 0)
	{
		LeftStickDirection.Y = Value;
	}
}

This is the left/right Right stick function:


void ATutorialCodeCharacter::TurnAtRate(float Rate)
{
	if ((Controller != NULL) && (Rate != 0.0f))
	{
		bRotating = true;
		RightStickDirection.X -= Rate;
	}

	if (Rate == 0)
	{
		RightStickDirection.X = Rate;
	}
}

this is the tick() with BOTH sticks included:


void ATutorialCodeCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	ATutorialCodeCharacter* MyCharacter = Cast<ATutorialCodeCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));

	FVector pPawnLoc = MyCharacter->GetActorLocation();

	if (LeftStickDirection.Y == 0 && LeftStickDirection.X == 0)
	{
		bMoving = false;
	}

	if (bMoving)
	{
		FRotator MyLeftStickRotator = LeftStickDirection.Rotation();

		MyLeftStickRotator.Yaw -= 90;

		const FRotator CameraRotation = FollowCamera->GetComponentRotation();
		const FRotator YawRotation(0, CameraRotation.Yaw, 0);

		MyLeftStickRotator.Yaw += CameraRotation.Yaw;

		FVector RunningRotationVector = MyLeftStickRotator.Vector();

		//Draw a line which is the direction pushed on the left stick.
		DrawDebugLine(GetWorld(), pPawnLoc, pPawnLoc + (RunningRotationVector * 300), FColor(0, 255, 0), false, 0, 10, 1);
	}


	if (RightStickDirection.Y == 0 && RightStickDirection.X == 0)
	{
		bRotating = false;
	}


	if (bRotating)
	{
		FRotator MyRightStickRotator = RightStickDirection.Rotation();

		MyRightStickRotator.Yaw -= 90;

		const FRotator CameraRotation = FollowCamera->GetComponentRotation();
		const FRotator YawRotation(0, CameraRotation.Yaw, 0);

		MyRightStickRotator.Yaw += CameraRotation.Yaw;

		FVector SpineRotationVector = MyRightStickRotator.Vector();

		//Draw a line which is the direction pushed on the right stick. 
		DrawDebugLine(GetWorld(), pPawnLoc, pPawnLoc + (SpineRotationVector * 300), FColor(255, 0, 0), false, 0, 10, 1);

               //Set the actor to face the direction pushed on the right stick
		SetActorRotation(FRotator(MyRightStickRotator));

	}	
}

Any advice would be great thanks.