Quick update, I recently updated the theme of the website. Now on to the Cool stuff, I have been spending my time figuring out the Unreal Engine recently and would like to show you guys what is happening. Above you can see a template for a twin stick shooter I am currently working on.
My objective this weekend was to create the movement mechanics and aiming mechanics. To do this I started with the TopDown Code template and replaced the onclick style gameplay from before to a direct input movement. This was easily achieved by using similar code to the movement code used in other template projects. The main area here was to figure out where our mouse is and its direction from the player. I did this by getting the rotation vector of the player world location and mouse world location.
//Find the Desired angle of rotation between the Player and Mouse FRotator PlayerDesiredRot = (PlayerLoc - MouseLoc).Rotation(); //Gets a Directional Vector Ranging from -1 to 1 in screenspace for the mouse from the character FVector MouseDirection = PlayerDesiredRot.Vector();
Using the player direction and mouse direction I obtained the angle from 0(Forward) - 180(Backward) from the player direction. This left me we having to detect if the mouse was on the right or left side of our player. Calcultating the distance between vectors allowed me to pick which value was the right or left angle.
//Gets player rotation FRotator PlayerRot = MyPawn->GetActorRotation(); //Gets the Directional Vector Ranging from -1 to 1 FVector PlayerDirection = PlayerRot.Vector()*-1; //Get The right vector for your character FRotator PlayerRightRot = PlayerDirection.Rotation(); PlayerRightRot.Add(0, 90, 0); FVector RightVector = PlayerRightRot.Vector(); //Gets the players current location minus the Z FVector PlayerLoc = MyPawn->GetActorLocation() * FVector(1, 1, 0); //The Aim at angle for the character MyPawn->AimAtAngle = ((acosf(FVector::DotProduct(PlayerDirection, MouseDirection))) * (180 / 3.1415926)); //Calculate the distances from all angles float RDist = FVector::Dist(MouseDirection, RightVector); float LDist = FVector::Dist(MouseDirection, RightVector*-1); float FDist = FVector::Dist(MouseDirection, PlayerDirection); float BDist = FVector::Dist(MouseDirection, PlayerDirection * -1);
To access my work and understand how I achieved this please feel free to download it from github https://github.com/EdwardCoughlan/TwinStickShooter.