Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "Ship.h"
  5.  
  6. // Sets default values
  7. AShip::AShip()
  8. {
  9.     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  10.     PrimaryActorTick.bCanEverTick = true;
  11.  
  12.     // |||| Variable Initialization |||||
  13.     Acceleration = 500.f;
  14.     TurnSpeed = 500.f;
  15.     MaxSpeed = 4000.f;
  16.     MinSpeed = 500.f;
  17.     CurrentForwardSpeed = 2000.f;
  18. }
  19.  
  20. // Called when the game starts or when spawned
  21. void AShip::BeginPlay()
  22. {
  23.     Super::BeginPlay();
  24.  
  25.    
  26. }
  27.  
  28. // Called every frame
  29. void AShip::Tick(float DeltaTime)
  30. {
  31.     const FVector LocalMove = FVector(CurrentForwardSpeed * DeltaTime, 0.f, 0.f);
  32.     AddActorLocalOffset(LocalMove, true);
  33.    
  34.     FRotator deltaRotation(0, 0, 0);
  35.     deltaRotation.Pitch = CurrentPitchSpeed * DeltaTime;
  36.     deltaRotation.Yaw = CurrentYawSpeed * DeltaTime;
  37.     deltaRotation.Roll = CurrentRollSpeed * DeltaTime;
  38.    
  39.     AddActorLocalRotation(deltaRotation);
  40.  
  41.     Super::Tick(DeltaTime);
  42. }
  43.  
  44. void AShip::MoveRight(float Val)
  45. {
  46.    
  47.     float targetYaw = Val * TurnSpeed; 
  48.     CurrentYawSpeed = FMath::FInterpTo(CurrentYawSpeed, targetYaw, GetWorld()->GetDeltaSeconds(), 2.0f);
  49.     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("targetYaw: %f"), targetYaw));
  50.     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("currentYawSpeed: %f"), CurrentYawSpeed));
  51.     bool isTurning = FMath::Abs(Val) > 0.2f;
  52.    
  53.     float targetRollSpeed = isTurning ? (CurrentYawSpeed * 0.5f) : (GetActorRotation().Roll * -2.0f);
  54.     CurrentRollSpeed = FMath::FInterpTo(CurrentRollSpeed, targetRollSpeed, GetWorld()->GetDeltaSeconds(), 2.0f);
  55. }
  56.  
  57. // Called to bind functionality to input
  58. void AShip::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  59. {
  60.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  61.     PlayerInputComponent->BindAxis("Right", this, &AShip::MoveRight);
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement