Advertisement
Guest User

Untitled

a guest
Sep 27th, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "KiritoCharacter.h"
  5. #include "GameFramework/SpringArmComponent.h"
  6. #include "GameFramework/CharacterMovementComponent.h"
  7. #include "Camera/CameraComponent.h"
  8. #define def(func) void AKiritoCharacter::func
  9. //i hate boiler plate code and duplication of code, which is why i made this definition
  10. // Sets default values
  11. AKiritoCharacter::AKiritoCharacter()
  12. {
  13. SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("spring arm component"));
  14. CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("camera component"));
  15. GetMesh()->SetRelativeLocationAndRotation(FVector(.0f, .0f, -90.0f), FQuat(FRotator(.0f, -90.0f, .0f)));
  16. SpringArmComp->SetupAttachment(GetMesh());
  17. CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
  18. SpringArmComp->bUsePawnControlRotation = true;
  19. GetCharacterMovement()->bOrientRotationToMovement = GetCharacterMovement()->bUseControllerDesiredRotation = GetCharacterMovement()->bIgnoreBaseRotation = true;
  20. PrimaryActorTick.bCanEverTick = true;
  21. }
  22.  
  23. // Called when the game starts or when spawned
  24. def(BeginPlay)()
  25. {
  26. Super::BeginPlay();
  27. }
  28.  
  29. // Called every frame
  30. def(Tick)(float DeltaTime)
  31. {
  32. Super::Tick(DeltaTime);
  33.  
  34. }
  35.  
  36. // Called to bind functionality to input
  37. def(SetupPlayerInputComponent)(UInputComponent* PlayerInputComponent)
  38. {
  39. Super::SetupPlayerInputComponent(PlayerInputComponent);
  40. PlayerInputComponent->BindAxis("MoveForward", this, &AKiritoCharacter::MoveForward);
  41. PlayerInputComponent->BindAxis("MoveRight", this, &AKiritoCharacter::MoveRight);
  42. PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  43. PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  44. PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  45. PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  46. PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &AKiritoCharacter::BeginCrouch);
  47. PlayerInputComponent->BindAction("Crouch", IE_Released, this, &AKiritoCharacter::EndCrouch);
  48. PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AKiritoCharacter::BeginSprint);
  49. PlayerInputComponent->BindAction("Sprint", IE_Released, this, &AKiritoCharacter::EndSprint);
  50. }
  51.  
  52. def(MoveForward)(float axisValue) {
  53. if (axisValue != .0f) {
  54. const FRotator YawRotation(.0, Controller->GetControlRotation().Yaw, .0);
  55. AddMovementInput(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X), axisValue);
  56. }
  57. }
  58. def(MoveRight)(float axisValue) {
  59. if (axisValue != .0f) {
  60. const FRotator YawRotation(.0, Controller->GetControlRotation().Yaw, .0);
  61. AddMovementInput(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y), axisValue);
  62. }
  63. }
  64. def(BeginSprint)() {
  65. GetCharacterMovement()->MaxWalkSpeed = 1000.0f;
  66. }
  67. def(EndSprint)() {
  68. GetCharacterMovement()->MaxWalkSpeed = 600.0f;
  69. }
  70. def(BeginCrouch)() {
  71. Crouch();
  72. }
  73. def(EndCrouch)() {
  74. UnCrouch();
  75. }
  76.  
  77. // Fill out your copyright notice in the Description page of Project Settings.
  78.  
  79. #pragma once
  80.  
  81. #include "CoreMinimal.h"
  82. #include "GameFramework/Character.h"
  83. #include "UsefulDefs.h"
  84. #include "KiritoCharacter.generated.h"
  85.  
  86. UCLASS()
  87. class IHATEUNREALENGINE_API AKiritoCharacter : public ACharacter
  88. {
  89. GENERATED_BODY()
  90.  
  91. public:
  92. // Sets default values for this character's properties
  93. AKiritoCharacter();
  94.  
  95. protected:
  96. UPROP_VA_BRW
  97. class USpringArmComponent* SpringArmComp;
  98. UPROP_VA_BRW
  99. class UCameraComponent* CameraComp;
  100. void BeginSprint();
  101. void EndSprint();
  102. void BeginCrouch();
  103. void EndCrouch();
  104. void MoveForward(float InputAxis);
  105. void MoveRight(float InputAxis);
  106. virtual void BeginPlay() override;
  107. public:
  108. // Called every frame
  109. virtual void Tick(float DeltaTime) override;
  110.  
  111. // Called to bind functionality to input
  112. virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
  113.  
  114. };
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement