Facebook
From Smelly Horse, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 123
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "GameFramework/Pawn.h"
  7. #include "Entropy_PlayerPawn.generated.h"
  8.  
  9. UCLASS()
  10. class ENTROPY_API AEntropy_PlayerPawn : public APawn
  11. {
  12.         GENERATED_BODY()
  13.  
  14. public:
  15.         // Sets default values for this pawn's properties
  16.         AEntropy_PlayerPawn();
  17.  
  18. protected:
  19.         // Called when the game starts or when spawned
  20.         virtual void BeginPlay() override;
  21.  
  22. public:
  23.         // Called every frame
  24.         virtual void Tick(float DeltaTime) override;
  25.  
  26.         // Called to bind functionality to input
  27.         virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
  28.  
  29.         UPROPERTY(EditAnywhere)
  30.         USceneComponent* SkeletalMesh;
  31.  
  32.         /** Camera boom positioning the camera behind the character */
  33.         UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
  34.         class USpringArmComponent* CameraBoom;
  35.  
  36.         /** Follow camera */
  37.         UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
  38.         class UCameraComponent* FollowCamera;
  39.  
  40.         //INPUT FUNCTIONS
  41.         void MoveForward (float AxisValue);
  42.         void MoveRight (float AxisValue);
  43.         void StartGrowing();
  44.         void StopGrowing();
  45.  
  46.  
  47.         //INPUT VARIABLES
  48.         FVector CurrentVelocity;
  49.  
  50.  
  51. };
  52.  
  53.   //
  54.  
  55.   // Fill out your copyright notice in the Description page of Project Settings.
  56.  
  57.  
  58. #include "Entropy_PlayerPawn.h"
  59. #include "Camera/CameraComponent.h"
  60. #include "Components/InputComponent.h"
  61. #include "Components/StaticMeshComponent.h"
  62. #include "Components/SkeletalMeshComponent.h"
  63.  
  64. #include "Components/CapsuleComponent.h"
  65. #include "GameFramework/SpringArmComponent.h"
  66.  
  67. // Sets default values
  68. AEntropy_PlayerPawn::AEntropy_PlayerPawn()
  69. {
  70.         // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  71.         PrimaryActorTick.bCanEverTick = true;
  72.  
  73.         // Set this pawn to be controlled by the lowest-numbered player
  74.         AutoPossessPlayer = EAutoReceiveInput::Player0;
  75.  
  76.         // Create a dummy root component we can attach things to.
  77.         RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  78.         // Create a camera and a visible object
  79.         //UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
  80.         SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMesh"));
  81.         SkeletalMesh->SetupAttachment(RootComponent);
  82.  
  83.         // Create a camera boom (pulls in towards the player if there is a collision)
  84.         CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  85.         CameraBoom->SetupAttachment(RootComponent);
  86.         CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character      
  87.         //CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  88.  
  89.         // Create a follow camera
  90.         FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  91.         FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  92.         FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  93.  
  94.  
  95. }
  96.  
  97. // Called when the game starts or when spawned
  98. void AEntropy_PlayerPawn::BeginPlay()
  99. {
  100.         Super::BeginPlay();
  101.        
  102. }
  103.  
  104. // Called every frame
  105. void AEntropy_PlayerPawn::Tick(float DeltaTime)
  106. {
  107.         Super::Tick(DeltaTime);
  108.  
  109.         //MOVEMENT
  110.  
  111.         if (!CurrentVelocity.IsZero())
  112.         {
  113.                 FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
  114.                 SetActorLocation(NewLocation);
  115.         }
  116.  
  117.  
  118. }
  119.  
  120. // Called to bind functionality to input
  121. void AEntropy_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  122. {
  123.         Super::SetupPlayerInputComponent(PlayerInputComponent);
  124.  
  125.        
  126.         //Bind Movement Input to Movement Functions
  127.         InputComponent->BindAxis("MoveForward", this, &AEntropy_PlayerPawn::MoveForward);
  128.         InputComponent->BindAxis("MoveRight", this, &AEntropy_PlayerPawn::MoveRight);
  129.  
  130.  
  131. }
  132.  
  133. void AEntropy_PlayerPawn::MoveForward(float AxisValue)
  134. {
  135.         //Move at 100 units per second forward or backward
  136.         CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 500.0f;
  137. }
  138.  
  139. void AEntropy_PlayerPawn::MoveRight(float AxisValue)
  140. {
  141.         //Move at 100 units per second right or left
  142.         CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 500.0f;
  143. }
  144.  
  145.  
  146.  
  147.