PasteBinPaster

Character_movement_cpp_file

Jun 7th, 2020
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.52 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "Theo_movement.h"
  5. #include "Components/InputComponent.h"
  6. #include "GameFramework/Controller.h"
  7. #include "GameFramework/CharacterMovementComponent.h"
  8. #include "Camera/CameraComponent.h"
  9. #include "GameFramework/Controller.h"
  10. //#include "Components/CapsuleComponent.h"
  11. #include "GameFramework/SpringArmComponent.h"
  12.  
  13.  
  14. // Sets default values
  15. ATheo_movement::ATheo_movement()
  16. {
  17.  
  18.     // set our turn rates for input
  19.     BaseTurnRate = 45.f;
  20.     BaseLookUpRate = 45.f;
  21.  
  22.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  23.     //PrimaryActorTick.bCanEverTick = true;
  24.  
  25.     //GetCharacterMovement()->bOrientRotationToMovement = true;
  26.     //not going to let the player rotate the character, going to let the camera rotate which rotates the character. (Otherwise the character would rotate infron of a non-rotating camera. (Pitch is x axis)
  27.     bUseControllerRotationPitch = false;
  28.     //not going to let the player rotate the character, going to let the camera rotate which rotates the character. (Otherwise the character would rotate infron of a non-rotating camera. (Pitch is z axis)
  29.     bUseControllerRotationYaw = false;
  30.     //not going to let the player rotate the character, going to let the camera rotate which rotates the character. (Otherwise the character would rotate infron of a non-rotating camera. (Pitch is x axis)
  31.     bUseControllerRotationRoll = false;
  32.  
  33.     UCharacterMovementComponent* const MovementComponent = GetCharacterMovement();
  34.     if (MovementComponent)
  35.     {
  36.         MovementComponent->bOrientRotationToMovement = true;
  37.         MovementComponent->bUseControllerDesiredRotation = false;
  38.     }
  39.  
  40.     //This orients the character in the direction he is moving. b is a boolean.
  41.     GetCharacterMovement()->bOrientRotationToMovement = true;
  42.     //Rotation rate F stands for float
  43.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
  44.     GetCharacterMovement()->JumpZVelocity = 600.0f;
  45.     //Control that we have when character is in air
  46.     GetCharacterMovement()->AirControl = 0.2f;
  47.  
  48.     //quotes in TEXT is a string
  49.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  50.     //Rootcomponent is the capsule component in character blueprint - we are attaching the camera boom/camera to it (also can do in character bp)
  51.     CameraBoom->SetupAttachment(RootComponent);
  52.     //How far camera is away from character
  53.     CameraBoom->TargetArmLength = 300.0f;
  54.     //Rotate arm based on the controller -similar to what we disabled above for pitch, yaw and roll.
  55.     CameraBoom->bUsePawnControlRotation = true;
  56.  
  57.     FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  58.     //Similar to attaching camera boom to root component. Camera is attached to boom which is what is holding the camera. Lets spring arm component do that for us.
  59.     FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
  60.  
  61.     //No control to the follow camera by the pawn b/c we are giving that power to the camera/spring arm/cameraboom.
  62.     FollowCamera->bUsePawnControlRotation = false;
  63.  
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70. // Called when the game starts or when spawned
  71. //void ATheo_movement::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  72. //void ATheo_movement::BeginPlay()
  73. /**{
  74.     Super::BeginPlay();
  75.  
  76. }
  77.  
  78. void ATheo_movement::LookUpAtRate(float Rate)
  79. {
  80. }
  81.  
  82. // Called every frame
  83. void ATheo_movement::Tick(float DeltaTime)
  84. {
  85.     Super::Tick(DeltaTime);
  86.  
  87. }
  88.  
  89. void ATheo_movement::StartJump()
  90. {
  91.     bPressedJump = true;
  92. }
  93.  
  94. void ATheo_movement::StopJump()
  95. {
  96.     bPressedJump = false;
  97. }
  98. */
  99. // Called to bind functionality to input
  100. void ATheo_movement::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  101. {
  102.     check(PlayerInputComponent);
  103.     //The playerinputcomponenet is from ue4 and is used to perform axis bindings. Player INPUT component. Input is what the user is doing to control the character.
  104.     //Super::SetupPlayerInputComponent(PlayerInputComponent);
  105.  
  106.     //Referencing player input component and using a 'arrow operator ->) to reference this variable. * is a pointer, which references the actual address in memory of what it is pointing to. -> is more logical to our brains b/c it is like a pointer but points to the variable/value associated
  107.     //with what it is pointing to. Ex inputcomponent->bindaxis(turn), is referencing the value of the turn string inside the parenthesis. If it was a * it would be accessing the address in memory of the actual turn variable, not the variable iteself. No idea how to use *.
  108.  
  109.     //This is meaning it is going to be used in 'this' class. Meaning the class it is being referenced in (character, pawn actor, whatever it is used in). In this case Theo_movement.
  110.  
  111.     //Since we have inherited from the Character ACharacter in the public clas (look in header file under UCLASS() where ACharacter is referenced...&APawn:: is used. Not sure how these two things correlate.
  112.     //Yaw input is the rotation on Zaxis. This is what axis the character rotates on if he were to turn. AddControllerYawInput allows him ot be controlled on yaw for the input function turn.
  113.     PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  114.     PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  115.  
  116.     PlayerInputComponent->BindAxis("LookUpRate", this, &ATheo_movement::LookUpAtRate);
  117.     PlayerInputComponent->BindAxis("TurnRate", this, &ATheo_movement::TurnAtRate);
  118.  
  119.     //Binding Jump
  120.     //IE_Pressed will detect when it is pressed, perhaps this is something that will need done for all action mappings?
  121.     //ACharacter::Jump is something that is already referenced within ue4 for jumping purposes, not yet sure what its deal is
  122.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  123.     //Released is how it knows when it the jump button is realesed...I would expect this means when it jumps, he can keep jumping until you release?
  124.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  125.  
  126.  
  127.     //binding the axis for movement. Referencing the actual Theo_movement class name, curious why this is something we did not referenced for turn/lookup.
  128.     PlayerInputComponent->BindAxis("MoveForward", this, &ATheo_movement::MoveForward);
  129.     PlayerInputComponent->BindAxis("MoveRight", this, &ATheo_movement::MoveRight);
  130.  
  131. }
  132.  
  133. void ATheo_movement::TurnAtRate(float Rate)
  134. {
  135.     // calculate delta for this frame from the rate information
  136.     AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  137. }
  138.  
  139. void ATheo_movement::LookUpAtRate(float Rate)
  140. {
  141.     // calculate delta for this frame from the rate information
  142.     AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  143. }
  144.  
  145.  
  146.  
  147. void ATheo_movement::MoveForward(float Value)
  148. {
  149.     if ((Controller != NULL) && (Value != 0.0f))
  150.     {
  151.     //Need to calculate where is the forward direction? Getting the rotation from the controller, below this we will specify actual rotation axis that will be contorlled and numbers associated.
  152.     const FRotator Rotation = Controller->GetControlRotation();
  153.     //In the parenthesis, firs is (x, y and z) Below, turn on yaw I thought was Z so that is confusing b/c Y is referenced as yaw when in fact that is Z. Maybe tutorial guy mispoke.
  154.     //I just realized that we are not just stating rotation (x,y,z), we are drilling down to yawrotation itself! This is a rotator function and the paremeters of yaw specifically...interesting.
  155.     const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
  156.     //Yawrotation below is being passed from the above statement. Vector is a variable that has xy and z. Rotation has xy and z as well. Get unit axis is getting the forward. X is forward. this gets us the forward direction of the player. In player blueprint, the forward arrow matches with the
  157.     //green x arrow.
  158.     const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  159.     //Movement input is self explanitory it passes the direction and passes the axis. Axis perameter- it will be passed depending on which way you go. ex forward is one and backward is negetive one. Whichever is being input it will be passed.
  160.     AddMovementInput(Direction, Value);
  161.     }
  162. }
  163.  
  164. //Only change from copied above is Y instead of X as the get unit axis.
  165. void ATheo_movement::MoveRight(float Value)
  166. {
  167.     if ((Controller != NULL) && (Value != 0.0f))
  168.     {
  169.         const FRotator Rotation = Controller->GetControlRotation();
  170.         const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
  171.  
  172.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  173.         AddMovementInput(Direction, Value);
  174.     }
  175. }
Add Comment
Please, Sign In to add comment