I am currently coding a Isometric/third person game ala Alien Swarm in C++. I need my ACharacter to face wherever it is I am pointing the mouse.
This blueprint seems to fir the bill :
However I am at a loss on finding the C++ equivalent of “Convert Mouse Location To World Space”. Can you folks point me to the right direction?
Thanks and sorry if this seems very elementar as I am very new to Unreal and game development in general.
Moss
(Moss)
October 14, 2014, 7:49am
2
The equivalent is the method within the PlayerController:
void DeprojectMousePositionToWorld(FVector & WorldLocation, FVector & WorldDirection) const;
Cheers,
Moss
6 Likes
I will have a go with it. Thanks for letting me know
Moss
(Moss)
October 14, 2014, 10:59am
4
If it’s what you needed remember to accept the answer so it might help others ^^
Excellent, I got what I need. One question though. The code where I now have to always make the character rotation to follow the cursor is located in the character’s Tick method while I have overridden. If this considered best practice?
Sorry if this question sounds a little stupid but this is my first time with Unreal or any game development thing.
Moss
(Moss)
October 14, 2014, 11:54am
6
It depends on where your changes are, could you post them?
Okay this is essentially what I did:
void AMyGameCharacterBase::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if (Controller != NULL)
{
FVector mouseLocation, mouseDirection;
AMyGamePlayerController* playerController = (AMyGamePlayerController*)GetWorld()->GetFirstPlayerController();
playerController->DeprojectMousePositionToWorld(mouseLocation, mouseDirection);
FRotator currentCharacterRotation = this->GetActorRotation();
FRotator targetRotation = mouseDirection.Rotation();
FRotator newRotation = FRotator(currentCharacterRotation.Pitch, targetRotation.Yaw, currentCharacterRotation.Roll);
this->SetActorRotation(newRotation);
}
}
Just wanna know if this is Kosher or if I am doing something really wonky here.
Already did, if it weren’t for you, I would be pulling my hair.
Moss
(Moss)
October 14, 2014, 1:20pm
10
I would better use the following methods to perform input rotations:
virtual void PlayerController::AddPitchInput(float Val);
virtual void PlayerController::AddYawInput(float Val);
This way if you lock the lock the look input it will affect you special rotation logic too, this is important when you lock the player in a cut-scene for example.
You can call those function from within your tick function.
Cheers,
Moss
Thanks for your input. Will do
sorry but I’m wondering if DeProjectMouseToWorld is used, is calculation like SingleLineTrace not necessary?
Six years later and this is still useful. Thanks
Moss
(Moss)
November 21, 2020, 9:21pm
15
Nice to see it’s still useful ^^. Time flies really.