当前位置:   article > 正文

【UE4学习】04——官方教程代码_ue4 某校不良生

ue4 某校不良生

实现平台:win8.1  UE4.10


按教程练习。

----------------------------------------------

1、实现Pawn移动(input)

MyPawn.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "GameFramework/Pawn.h"
  4. #include "MyPawn.generated.h"
  5. UCLASS()
  6. class MYPROJECT2_BP_API AMyPawn : public APawn
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this pawn's properties
  11. AMyPawn();
  12. // Called when the game starts or when spawned
  13. virtual void BeginPlay() override;
  14. // Called every frame
  15. virtual void Tick( float DeltaSeconds ) override;
  16. // Called to bind functionality to input
  17. virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
  18. UPROPERTY(EditAnywhere)
  19. USceneComponent* OurVisibleComponent;
  20. void MoveForward(float Value);
  21. void MoveRight(float Value);
  22. void Bigger();
  23. void Smaller();
  24. bool bGrowing;
  25. FVector CurrentVelocity;
  26. };
Mypawn.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "MyProject2_BP.h"
  3. #include "MyPawn.h"
  4. // Sets default values
  5. AMyPawn::AMyPawn()
  6. {
  7. // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9. AutoPossessPlayer = EAutoReceiveInput::Player0;
  10. RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));
  11. UCameraComponent* Mycamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
  12. OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
  13. Mycamera->AttachTo(RootComponent);
  14. Mycamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
  15. Mycamera->SetRelativeRotation(FRotator(-45.0f, 0, 0));
  16. OurVisibleComponent->AttachTo(RootComponent);
  17. }
  18. // Called when the game starts or when spawned
  19. void AMyPawn::BeginPlay()
  20. {
  21. Super::BeginPlay();
  22. }
  23. // Called every frame
  24. void AMyPawn::Tick( float DeltaTime )
  25. {
  26. Super::Tick( DeltaTime );
  27. float CurrentScale = OurVisibleComponent->GetComponentScale().X;
  28. if (bGrowing)
  29. {
  30. // 在一秒的时间内增长到两倍的大小
  31. CurrentScale += DeltaTime;
  32. }
  33. else
  34. {
  35. // 随着增长收缩到一半
  36. CurrentScale -= (DeltaTime * 0.5f);
  37. }
  38. // 确认永不低于起始大小,或增大之前的两倍大小。
  39. CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
  40. OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
  41. // 基于"MoveX"和 "MoveY"坐标轴来处理移动
  42. if (!CurrentVelocity.IsZero())
  43. {
  44. FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
  45. SetActorLocation(NewLocation);
  46. }
  47. }
  48. // Called to bind functionality to input
  49. void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  50. {
  51. Super::SetupPlayerInputComponent(InputComponent);
  52. InputComponent->BindAction("Bigger", IE_Pressed, this, &AMyPawn::Bigger);
  53. InputComponent->BindAction("Smaller", IE_Pressed, this, &AMyPawn::Smaller);
  54. InputComponent->BindAxis("MoveForward", this, &AMyPawn::MoveForward);
  55. InputComponent->BindAxis("MoveRight", this, &AMyPawn::MoveRight);
  56. }
  57. void AMyPawn::MoveForward(float Value)
  58. {
  59. CurrentVelocity.X = Value * 100.0f;
  60. }
  61. void AMyPawn::MoveRight(float Value)
  62. {
  63. CurrentVelocity.Y = Value * 100.0f;
  64. }
  65. void AMyPawn::Bigger()
  66. {
  67. bGrowing = true;
  68. }
  69. void AMyPawn::Smaller()
  70. {
  71. bGrowing = false;
  72. }


==============================================================================

2、实现相机自动切换(注意需要两个camera)

CameraDirector.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "GameFramework/Actor.h"
  4. #include "CameraDirector.generated.h"
  5. UCLASS()
  6. class MYPROJECT2_BP_API ACameraDirector : public AActor
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this actor's properties
  11. ACameraDirector();
  12. // Called when the game starts or when spawned
  13. virtual void BeginPlay() override;
  14. // Called every frame
  15. virtual void Tick( float DeltaSeconds ) override;
  16. UPROPERTY(EditAnywhere)
  17. AActor* CameraOne;
  18. UPROPERTY(EditAnywhere)
  19. AActor* CameraTwo;
  20. float TimeToNextCameraChange;
  21. };

CameraDirector.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "MyProject2_BP.h"
  3. #include "CameraDirector.h"
  4. #include "Kismet/GameplayStatics.h"
  5. // Sets default values
  6. ACameraDirector::ACameraDirector()
  7. {
  8. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  9. PrimaryActorTick.bCanEverTick = true;
  10. }
  11. // Called when the game starts or when spawned
  12. void ACameraDirector::BeginPlay()
  13. {
  14. Super::BeginPlay();
  15. }
  16. // Called every frame
  17. void ACameraDirector::Tick( float DeltaTime )
  18. {
  19. Super::Tick( DeltaTime );
  20. const float TimeBetweenCameraChanges = 2.0f;
  21. const float SmoothBlendTime = 0.75;
  22. TimeToNextCameraChange -= DeltaTime;
  23. if (TimeToNextCameraChange<=0.0f)
  24. {
  25. TimeToNextCameraChange += TimeBetweenCameraChanges;
  26. APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
  27. if (OurPlayerController)
  28. {
  29. if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
  30. {
  31. // 立即切换到相机1。
  32. OurPlayerController->SetViewTarget(CameraOne);
  33. }
  34. else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
  35. {
  36. // 平滑地混合到相机2。
  37. OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
  38. }
  39. }
  40. }
  41. }

==============================================================================


3、实现倒计时(timer)(注意宏的用法、C++与bluepringt交互)

CountDown.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "GameFramework/Actor.h"
  4. #include "CountDown.generated.h"
  5. UCLASS()
  6. class MYPROJECT2_BP_API ACountDown : public AActor
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this actor's properties
  11. ACountDown();
  12. // Called when the game starts or when spawned
  13. virtual void BeginPlay() override;
  14. // Called every frame
  15. virtual void Tick( float DeltaSeconds ) override;
  16. UPROPERTY(EditAnywhere, Category = "CountNumber")
  17. int32 CountDownTime;
  18. UTextRenderComponent* CountDownText;
  19. void UpdataTimerDisplay();
  20. void AdvanceTimer();
  21. UFUNCTION(BlueprintNativeEvent, Category = "CountNumber")
  22. void CountdownHasFinish();
  23. virtual void CountdownHasFinish_Implementation();
  24. FTimerHandle CountdownTimerhandle;
  25. };

CountDown.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "MyProject2_BP.h"
  3. #include "CountDown.h"
  4. // Sets default values
  5. ACountDown::ACountDown()
  6. {
  7. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9. CountDownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
  10. CountDownText->SetHorizontalAlignment(EHTA_Center);
  11. CountDownText->SetWorldSize(150.0f);
  12. RootComponent = CountDownText;
  13. CountDownTime = 3.0f;
  14. }
  15. void ACountDown::UpdataTimerDisplay()
  16. {
  17. CountDownText->SetText((FString::FromInt(FMath::Max(CountDownTime, 0))));
  18. }
  19. void ACountDown::AdvanceTimer()
  20. {
  21. --CountDownTime;
  22. UpdataTimerDisplay();
  23. if (CountDownTime<1)
  24. {
  25. GetWorldTimerManager().ClearTimer(CountdownTimerhandle);
  26. CountdownHasFinish();
  27. }
  28. }
  29. //void ACountDown::CountdownHasFinish()
  30. //{
  31. // //CountDownText->SetText(TEXT("GO!"));
  32. //}
  33. void ACountDown::CountdownHasFinish_Implementation()
  34. {
  35. //Change to a special readout
  36. CountDownText->SetText(TEXT("GO!"));
  37. }
  38. // Called when the game starts or when spawned
  39. void ACountDown::BeginPlay()
  40. {
  41. Super::BeginPlay();
  42. UpdataTimerDisplay();
  43. GetWorldTimerManager().SetTimer(CountdownTimerhandle, this, &ACountDown::AdvanceTimer, 1.0f, true);
  44. }
  45. // Called every frame
  46. void ACountDown::Tick( float DeltaTime )
  47. {
  48. Super::Tick( DeltaTime );
  49. }

==============================================================================

4、Pwan与碰撞(粒子特效) 注意碰撞时,按下space才会产生火焰

ColisionPawn.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "GameFramework/Pawn.h"
  4. #include "ColisionPawn.generated.h"
  5. UCLASS()
  6. class MYPROJECT2_BP_API AColisionPawn : public APawn
  7. {
  8. GENERATED_BODY()
  9. public:
  10. // Sets default values for this pawn's properties
  11. AColisionPawn();
  12. // Called when the game starts or when spawned
  13. virtual void BeginPlay() override;
  14. // Called every frame
  15. virtual void Tick( float DeltaSeconds ) override;
  16. // Called to bind functionality to input
  17. virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
  18. UParticleSystemComponent* MyParticleSystem;
  19. class UColisionPawnMovementComponent* MyMovementComponent;
  20. virtual UPawnMovementComponent* GetMovementComponent() const override;
  21. void MoveForward(float AxisValue);
  22. void MoveRight(float AxisValue);
  23. void Turn(float AxisValue);
  24. void ParticleToggle();
  25. };
ColisionPawn.cpp
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "MyProject2_BP.h"
  3. #include "ColisionPawn.h"
  4. #include "ColisionPawnMovementComponent.h"
  5. // Sets default values
  6. AColisionPawn::AColisionPawn()
  7. {
  8. // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  9. PrimaryActorTick.bCanEverTick = true;
  10. // Our root component will be a sphere that reacts to physics
  11. USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
  12. RootComponent = SphereComponent;
  13. SphereComponent->InitSphereRadius(40.f);
  14. SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
  15. // Create and position a mesh component so we can see where our sphere is
  16. UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
  17. SphereVisual->AttachTo(SphereComponent);
  18. static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
  19. if (SphereVisualAsset.Succeeded())
  20. {
  21. SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
  22. SphereVisual->SetRelativeLocation(FVector(0, 0, 0));
  23. SphereVisual->SetWorldScale3D(FVector(0.8f));
  24. }
  25. // Create a particle system that we can activate or deactivate
  26. MyParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticle"));
  27. MyParticleSystem->AttachTo(SphereVisual);
  28. MyParticleSystem->bAutoActivate = false;
  29. MyParticleSystem->SetRelativeLocation(FVector(-20.0f, 0.0f, 20.0f));
  30. static ConstructorHelpers::FObjectFinder<UParticleSystem> ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
  31. if (ParticleAsset.Succeeded())
  32. {
  33. MyParticleSystem->SetTemplate(ParticleAsset.Object);
  34. }
  35. // Use a spring arm to give the camera smooth, natural-feeling motion.
  36. USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm"));
  37. SpringArm->AttachTo(RootComponent);
  38. SpringArm->RelativeRotation = FRotator(-45.f, 0.f, 0.f);
  39. SpringArm->TargetArmLength = 400.0f;
  40. SpringArm->bEnableCameraLag = true;
  41. SpringArm->CameraLagSpeed = 3.0f;
  42. // Create a camera and attach to our spring arm
  43. UCameraComponent* MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("AcrualCamera"));
  44. MyCamera->AttachTo(SpringArm, USpringArmComponent::SocketName);
  45. // Take control of the default player
  46. AutoPossessPlayer = EAutoReceiveInput::Player0;
  47. // Create an instance of our movement component, and tell it to update the root.
  48. MyMovementComponent = CreateDefaultSubobject<UColisionPawnMovementComponent>(TEXT("CustomMovementComponent"));
  49. MyMovementComponent->UpdatedComponent = RootComponent;
  50. }
  51. UPawnMovementComponent* AColisionPawn::GetMovementComponent() const
  52. {
  53. return MyMovementComponent;
  54. }
  55. void AColisionPawn::MoveForward(float AxisValue)
  56. {
  57. if (MyMovementComponent && (MyMovementComponent->UpdatedComponent == RootComponent))
  58. {
  59. MyMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
  60. }
  61. }
  62. void AColisionPawn::MoveRight(float AxisValue)
  63. {
  64. if (MyMovementComponent && (MyMovementComponent->UpdatedComponent == RootComponent))
  65. {
  66. MyMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
  67. }
  68. }
  69. void AColisionPawn::Turn(float AxisValue)
  70. {
  71. FRotator NewRotation = GetActorRotation();
  72. NewRotation.Yaw += AxisValue;
  73. SetActorRotation(NewRotation);
  74. }
  75. void AColisionPawn::ParticleToggle()
  76. {
  77. if (MyParticleSystem && MyParticleSystem->Template)
  78. {
  79. MyParticleSystem->ToggleActive();
  80. }
  81. }
  82. // Called when the game starts or when spawned
  83. void AColisionPawn::BeginPlay()
  84. {
  85. Super::BeginPlay();
  86. }
  87. // Called every frame
  88. void AColisionPawn::Tick( float DeltaTime )
  89. {
  90. Super::Tick( DeltaTime );
  91. }
  92. // Called to bind functionality to input
  93. void AColisionPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  94. {
  95. Super::SetupPlayerInputComponent(InputComponent);
  96. InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &AColisionPawn::ParticleToggle);
  97. InputComponent->BindAxis("MoveForward", this, &AColisionPawn::MoveForward);
  98. InputComponent->BindAxis("MoveRight", this, &AColisionPawn::MoveRight);
  99. InputComponent->BindAxis("Turn", this, &AColisionPawn::Turn);
  100. }


ColisionPawnMovementComponent.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "GameFramework/PawnMovementComponent.h"
  4. #include "ColisionPawnMovementComponent.generated.h"
  5. /**
  6. *
  7. */
  8. UCLASS()
  9. class MYPROJECT2_BP_API UColisionPawnMovementComponent : public UPawnMovementComponent
  10. {
  11. GENERATED_BODY()
  12. public:
  13. virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction);
  14. };

ColisionPawnMovementComponent.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "MyProject2_BP.h"
  3. #include "ColisionPawnMovementComponent.h"
  4. void UColisionPawnMovementComponent::TickComponent(float DeltaTime,
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/261008
推荐阅读