00 项目设置:FPS C++ 带初学者内容
01 FPS_Cpp_WtProjectile(一般加了中文注释才是新添加的代码,其它都是文件自带的)
FPS,第一人称设计
Cpp,带C++类
Wt,With Starter Cntent,带初学者内容
01 01 FPS_Cpp_WtProjectile.h
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Particles/ParticleSystem.h"
#include "FPS_Cpp_WtProjectile.generated.h"
UCLASS(config=Game)
class AFPS_Cpp_WtProjectile : public AActor
{
GENERATED_BODY()
/** Sphere collision component */
UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
class USphereComponent* CollisionComp;
/** Projectile movement component */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
class UProjectileMovementComponent* ProjectileMovement;
public:
AFPS_Cpp_WtProjectile();
/** called when projectile hits something */
UFUNCTION()
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
/** Returns CollisionComp subobject **/
FORCEINLINE class USphereComponent* GetCollisionComp() const { return CollisionComp; }
/** Returns ProjectileMovement subobject **/
FORCEINLINE class UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
//不赋死值
protected:
//EditDefaultOnly少了s
UPROPERTY(EditDefaultsOnly,Category="Emitter")
//#include "Particles/ParticleSystem.h"
UParticleSystem* Emitter_Projectile;
};
01 02 FPS_Cpp_WtProjectile.cpp
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "FPS_Cpp_WtProjectile.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
AFPS_Cpp_WtProjectile::AFPS_Cpp_WtProjectile()
{
// Use a sphere as a simple collision representation
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
CollisionComp->InitSphereRadius(5.0f);
CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
CollisionComp->OnComponentHit.AddDynamic(this, &AFPS_Cpp_WtProjectile::OnHit); // set up a notification for when this component hits something blocking
// Players can't walk on it
CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
CollisionComp->CanCharacterStepUpOn = ECB_No;
// Set as root component
RootComponent = CollisionComp;
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 3000.f;
ProjectileMovement->MaxSpeed = 3000.f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->bShouldBounce = true;
// Die after 3 seconds by default
InitialLifeSpan = 3.0f;
}
//************************************************************
void AFPS_Cpp_WtProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
// Only add impulse and destroy projectile if we hit a physics
if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL) && OtherComp->IsSimulatingPhysics())
{
OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
Destroy();
}
/*
UParticleSystemComponent* UGameplayStatics::SpawnEmitterAtLocation(
const UObject* WorldContextObject, 关卡地址
UParticleSystem* EmitterTemplate,
FVector SpawnLocation,
FRotator SpawnRotation,
bool bAutoDestroy,
EPSCPoolMethod PoolingMethod,
bool bAutoActivateSystem)
{
return SpawnEmitterAtLocation(
WorldContextObject,
EmitterTemplate,
SpawnLocation,
SpawnRotation,
FVector(1.f),
bAutoDestroy,
PoolingMethod,
bAutoActivateSystem
);
}
*/
//特定位置加特效,特效名称,特效位置,自动销毁(默认true)
/** Getter for the cached world pointer, will return null if the actor is not actually spawned in a level */
//virtual UWorld* GetWorld() const override;//GameFramework/Actor.h
//UGameplayStatics需要Kismet/GameplayStatics.h
//三个重载
//也行,UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), Emitter_Projectile, GetActorLocation());
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), Emitter_Projectile, this->GetTransform());
}