// Fill out your copyright notice in the Description page of Project Settings.#include"MyActor_One.h"#include"Engine/Engine.h"#include"UObject/ConstructorHelpers.h"#include"Components/StaticMeshComponent.h"#include"Components/SphereComponent.h"#include"Particles/ParticleSystemComponent.h"#include"MyObjectUE5/MyCharacters/MyCharacter.h"#include"Kismet/GamePlayStatics.h"#include"Sound/SoundCue.h"// Sets default valuesAMyActor_One::AMyActor_One(){// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick =true;
StaticMesh =CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
RootComponent = StaticMesh;
TriggerVolume =CreateDefaultSubobject<USphereComponent>(TEXT("TriggerVolume"));
TriggerVolume->SetupAttachment(GetRootComponent());
ParticleEffectsComponent =CreateDefaultSubobject<UParticleSystemComponent>(TEXT("ParticleEffects"));
ParticleEffectsComponent->SetupAttachment(GetRootComponent());//设置TriggerVolume碰撞的硬编码
TriggerVolume->SetCollisionEnabled(ECollisionEnabled::QueryOnly);//设置碰撞类型
TriggerVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);//设置对象移动时其应视为某种物体
TriggerVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);//设置所有的碰撞响应为忽略
TriggerVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);//设置Pawn碰撞响应为重叠}// Called when the game starts or when spawnedvoidAMyActor_One::BeginPlay(){Super::BeginPlay();
TriggerVolume->OnComponentBeginOverlap.AddDynamic(this,&AMyActor_One::OnOverlapBegin);
TriggerVolume->OnComponentEndOverlap.AddDynamic(this,&AMyActor_One::OnOverlapEnd);}voidAMyActor_One::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,bool bFromSweep,const FHitResult& SweepResult){if(OtherActor){
AMyCharacter* Player =Cast<AMyCharacter>(OtherActor);if(Player){if(Particle){UGameplayStatics::SpawnEmitterAtLocation(this, Particle,GetActorLocation(),FRotator(0.f),true);}if(Sound){UGameplayStatics::PlaySound2D(this, Sound);}Destroy();}}}voidAMyActor_One::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex){}// Called every framevoidAMyActor_One::Tick(float DeltaTime){Super::Tick(DeltaTime);if(bRotate){
FRotator rotator =GetActorRotation();
rotator.Yaw += RotationRate * DeltaTime;SetActorRotation(rotator);}}
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Character.h"#include"InputActionValue.h"#include"MyCharacter.generated.h"UCLASS()classMYOBJECTUE5_API AMyCharacter :public ACharacter
{GENERATED_BODY()public:// Sets default values for this character's propertiesAMyCharacter();UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category ="Camera", meta =(AllPrivateAccess ="true"))classUSpringArmComponent* SpringArm;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category ="Camera", meta =(AllPrivateAccess ="true"))classUCameraComponent* MyCamera;//映射绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input", meta =(AllPrivateAccess ="true"))classUInputMappingContext* DefaultMappingContext;//移动绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input", meta =(AllPrivateAccess ="true"))classUInputAction* MoveAction;//视角绑定UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Input", meta =(AllPrivateAccess ="true"))classUInputAction* LookAction;UPROPERTY(EditAnywhere, BlueprintReadOnly, Category ="Coin")int Coin =0;protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;voidCharacterMove(const FInputActionValue& value);voidCharacterLook(const FInputActionValue& value);public:// Called every framevirtualvoidTick(float DeltaTime)override;// Called to bind functionality to inputvirtualvoidSetupPlayerInputComponent(classUInputComponent* PlayerInputComponent)override;};
MyCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"MyCharacter.h"#include"Camera/CameraComponent.h"#include"GameFramework/SpringArmComponent.h"#include"GameFramework/CharacterMovementComponent.h"#include"EnhancedInputComponent.h"#include"EnhancedInputSubsystems.h"#include"Engine/Engine.h"#include"MyObjectUE5/MyActors/MyActor_One.h"// Sets default valuesAMyCharacter::AMyCharacter(){// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick =true;GetCharacterMovement()->bOrientRotationToMovement =true;GetCharacterMovement()->RotationRate =FRotator(0.f,500.f,0.f);GetCharacterMovement()->MaxWalkSpeed =500.f;GetCharacterMovement()->MinAnalogWalkSpeed =20.f;GetCharacterMovement()->BrakingDecelerationWalking =2000.f;//相机臂
SpringArm =CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(GetRootComponent());
SpringArm->TargetArmLength =400.f;
SpringArm->bUsePawnControlRotation =true;//相机
MyCamera =CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);//附加到末尾
MyCamera->bUsePawnControlRotation =false;}// Called when the game starts or when spawnedvoidAMyCharacter::BeginPlay(){Super::BeginPlay();
APlayerController* PlayerController =Cast<APlayerController>(Controller);if(PlayerController){
UEnhancedInputLocalPlayerSubsystem* Subsystem =
ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());if(Subsystem){//映射到上下文
Subsystem->AddMappingContext(DefaultMappingContext,0);}}}voidAMyCharacter::CharacterMove(const FInputActionValue& value){
FVector2D MovementVector = value.Get<FVector2D>();//获取速度if(Controller!=nullptr){
FRotator Rotation = Controller->GetControlRotation();
FRotator YawRotation =FRotator(0, Rotation.Yaw,0);//获取到前后单位向量
FVector ForwardDirection =FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);//获取左右单位向量
FVector RightDirection =FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);AddMovementInput(ForwardDirection, MovementVector.Y);AddMovementInput(RightDirection, MovementVector.X);}}voidAMyCharacter::CharacterLook(const FInputActionValue& value){
FVector2D LookAxisVector = value.Get<FVector2D>();if(Controller !=nullptr){//GEngine->AddOnScreenDebugMessage(1, 10, FColor::Red, FString::Printf(TEXT("%f"),(GetControlRotation().Pitch)));AddControllerYawInput(LookAxisVector.X);if(GetControlRotation().Pitch <270.f&&GetControlRotation().Pitch>180.f&& LookAxisVector.Y >0.f){return;}if(GetControlRotation().Pitch <180.f&&GetControlRotation().Pitch>45.f&& LookAxisVector.Y <0.f){return;}AddControllerPitchInput(LookAxisVector.Y);}}// Called every framevoidAMyCharacter::Tick(float DeltaTime){Super::Tick(DeltaTime);}// Called to bind functionality to inputvoidAMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);
UEnhancedInputComponent* EnhancedInputComponent =Cast<UEnhancedInputComponent>(PlayerInputComponent);if(EnhancedInputComponent){//移动绑定
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered,this,&AMyCharacter::CharacterMove);
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered,this,&AMyCharacter::CharacterLook);}}
MyActor_One.h
// Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"CoreMinimal.h"#include"GameFramework/Actor.h"#include"MyActor_One.generated.h"UCLASS()classMYOBJECTUE5_API AMyActor_One :public AActor
{GENERATED_BODY()public:// Sets default values for this actor's propertiesAMyActor_One();UPROPERTY(VisibleAnywhere, BlueprintReadOnly)classUStaticMeshComponent* StaticMesh;UPROPERTY(VisibleAnywhere, BlueprintReadWrite)classUSphereComponent* TriggerVolume;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Interoperable|Particle")classUParticleSystemComponent* ParticleEffectsComponent;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Interoperable|Particle")classUParticleSystem* Particle;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Interoperable|Sounds")classUSoundCue* Sound;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Interoperable Item|Properties")bool bRotate =true;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category ="Interoperable Item|Properties")float RotationRate =45.f;protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay()override;UFUNCTION()voidOnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,bool bFromSweep,const FHitResult& SweepResult);UFUNCTION()voidOnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);public:// Called every framevirtualvoidTick(float DeltaTime)override;};
MyActor_One.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include"MyActor_One.h"#include"Engine/Engine.h"#include"UObject/ConstructorHelpers.h"#include"Components/StaticMeshComponent.h"#include"Components/SphereComponent.h"#include"Particles/ParticleSystemComponent.h"#include"MyObjectUE5/MyCharacters/MyCharacter.h"#include"Kismet/GamePlayStatics.h"#include"Sound/SoundCue.h"// Sets default valuesAMyActor_One::AMyActor_One(){// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick =true;
StaticMesh =CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
RootComponent = StaticMesh;
TriggerVolume =CreateDefaultSubobject<USphereComponent>(TEXT("TriggerVolume"));
TriggerVolume->SetupAttachment(GetRootComponent());
ParticleEffectsComponent =CreateDefaultSubobject<UParticleSystemComponent>(TEXT("ParticleEffects"));
ParticleEffectsComponent->SetupAttachment(GetRootComponent());//设置TriggerVolume碰撞的硬编码
TriggerVolume->SetCollisionEnabled(ECollisionEnabled::QueryOnly);//设置碰撞类型
TriggerVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);//设置对象移动时其应视为某种物体
TriggerVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);//设置所有的碰撞响应为忽略
TriggerVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);//设置Pawn碰撞响应为重叠}// Called when the game starts or when spawnedvoidAMyActor_One::BeginPlay(){Super::BeginPlay();
TriggerVolume->OnComponentBeginOverlap.AddDynamic(this,&AMyActor_One::OnOverlapBegin);
TriggerVolume->OnComponentEndOverlap.AddDynamic(this,&AMyActor_One::OnOverlapEnd);}voidAMyActor_One::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,bool bFromSweep,const FHitResult& SweepResult){if(OtherActor){
AMyCharacter* Player =Cast<AMyCharacter>(OtherActor);if(Player){if(Particle){UGameplayStatics::SpawnEmitterAtLocation(this, Particle,GetActorLocation(),FRotator(0.f),true);}if(Sound){UGameplayStatics::PlaySound2D(this, Sound);}
Player->Coin++;
GEngine->AddOnScreenDebugMessage(2,10, FColor::Red,FString::Printf(TEXT("%d"), Player->Coin));Destroy();}}}voidAMyActor_One::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex){}// Called every framevoidAMyActor_One::Tick(float DeltaTime){Super::Tick(DeltaTime);if(bRotate){
FRotator rotator =GetActorRotation();
rotator.Yaw += RotationRate * DeltaTime;SetActorRotation(rotator);}}