Character.h:
- // Fill out your copyright notice in the Description page of Project Settings.
-
- #pragma once
-
- #include "GameFramework/SpringArmComponent.h"
- #include "Camera/CameraComponent.h"
- #include "CoreMinimal.h"
- #include "GameFramework/Character.h"
- #include "SCharacter.generated.h"
-
- UCLASS()
- class SPECIAL_API ASCharacter : public ACharacter
- {
- GENERATED_BODY()
-
- public:
- // Sets default values for this character's properties
- ASCharacter();
-
- protected:
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
- UPROPERTY(VisibleAnywhere)
- UCameraComponent* CameraComp;
-
- UPROPERTY(VisibleAnywhere)
- USpringArmComponent* SpringArmComp;
-
- UPROPERTY(EditAnywhere)
- TSubclassOf
ProjectileClass; -
- public:
- // Called every frame
- virtual void Tick(float DeltaTime) override;
-
- // Called to bind functionality to input
- virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
-
- void MoveForward(float Value);
-
- void MoveRight(float Value);
-
- void PrimaryAttack();
- };
Character.cpp:
- // Fill out your copyright notice in the Description page of Project Settings.
-
-
- #include "SCharacter.h"
- #include "GameFramework/characterMovementComponent.h"
-
- // Sets default values
- ASCharacter::ASCharacter()
- {
- // 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;
-
- CameraComp = CreateDefaultSubobject
("Camera"); - SpringArmComp = CreateDefaultSubobject
("SpringArm"); - SpringArmComp->SetupAttachment(RootComponent);
- CameraComp->SetupAttachment(SpringArmComp);
- SpringArmComp->SetRelativeLocationAndRotation(FVector(30.0f, 0.0f, 20.0f), FRotator(-30.0f, 0.0f, 0.0f));
- CameraComp->bUsePawnControlRotation = true;
- this->bUseControllerRotationYaw = false;
-
- GetCharacterMovement()->bOrientRotationToMovement = true;
- }
-
- // Called when the game starts or when spawned
- void ASCharacter::BeginPlay()
- {
- Super::BeginPlay();
-
- }
-
- // Called every frame
- void ASCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
-
- }
-
- void ASCharacter::MoveForward(float Value) {
- FRotator ControlRot = GetControlRotation();
- ControlRot.Pitch = 0.0f;
- ControlRot.Roll = 0.0f;
- AddMovementInput(ControlRot.Vector(), Value);
-
- return;
- }
-
- void ASCharacter::MoveRight(float Value) {
- FRotator ControlRot = GetControlRotation();
- ControlRot.Pitch = 0.0f;
- ControlRot.Roll = 0.0f;
- FVector RightVector = FRotationMatrix(ControlRot).GetScaledAxis(EAxis::Y);
- AddMovementInput(RightVector, Value);
-
- return;
- }
-
- void ASCharacter::PrimaryAttack()
- {
- FVector HandLocation = GetMesh()->GetSocketLocation("spine_03");
- FTransform SpawnTM = FTransform(GetControlRotation(), HandLocation);
-
- FActorSpawnParameters SpawnParams;
- SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
-
- GetWorld()->SpawnActor
(ProjectileClass, SpawnTM, SpawnParams); - return;
- }
-
-
- // Called to bind functionality to input
- void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
-
- PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
-
- PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
-
- PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
-
- PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
-
- PlayerInputComponent->BindAction("PrimaryAttack", IE_Pressed, this, &ASCharacter::PrimaryAttack);
-
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
-
-
- }
-
Bullet.h:
- // Fill out your copyright notice in the Description page of Project Settings.
-
- #pragma once
-
- #include "CoreMinimal.h"
- #include "GameFramework/Actor.h"
- #include "SMagicProjectile.generated.h"
-
- class USphereComponent;
- class UProjectileMovementComponent;
- class UParticleSystemComponent;
-
- UCLASS()
- class SPECIAL_API ASMagicProjectile : public AActor
- {
- GENERATED_BODY()
-
- public:
- // Sets default values for this actor's properties
- ASMagicProjectile();
-
- protected:
-
- UPROPERTY(VisibleAnywhere)
- USphereComponent* SphereComp;
-
- UPROPERTY(VisibleAnywhere)
- UProjectileMovementComponent* MovementComp;
-
- UPROPERTY(VisibleAnywhere)
- UParticleSystemComponent* EffectComp;
-
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
-
- public:
- // Called every frame
- virtual void Tick(float DeltaTime) override;
-
- void DestroyMyself();
-
-
- };
Bullet.cpp:
- // Fill out your copyright notice in the Description page of Project Settings.
-
-
- #include "SMagicProjectile.h"
- #include "GameFramework/ProjectileMovementComponent.h"
- #include "particles/ParticleSystemComponent.h"
- #include "Components/SphereComponent.h"
-
- // Sets default values
- ASMagicProjectile::ASMagicProjectile()
- {
- // 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;
-
- SphereComp = CreateDefaultSubobject
("SphereComp"); - RootComponent = SphereComp;
-
- EffectComp = CreateDefaultSubobject
("EffectComp"); - EffectComp->SetupAttachment(SphereComp);
-
- MovementComp = CreateDefaultSubobject
("MoveComp"); - MovementComp->InitialSpeed = 0.0f;
- MovementComp->bRotationFollowsVelocity = true;
- MovementComp->bInitialVelocityInLocalSpace = true;
- MovementComp->ProjectileGravityScale = 0;
-
- }
-
- // Called when the game starts or when spawned
- void ASMagicProjectile::BeginPlay()
- {
- Super::BeginPlay();
- FTimerHandle Handle;
- GetWorld()->GetTimerManager().SetTimer(Handle, this, &ASMagicProjectile::DestroyMyself, 3.0f, false);
-
- }
-
- // Called every frame
- void ASMagicProjectile::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
-
- }
-
- void ASMagicProjectile::DestroyMyself()
- {
- this->Destroy();
- }
-
cast to:
APawn* MyPawn = Cast
射线检测:
FHitResult Hit;
FCollisionObjectQueryParams objectQueryParams;
objectQueryParams.AddObjectTypesToQuery(ECC_WorldDynamic);
AActor* MyOwner = GetOwner();
FVector Start;
FVector End;
FVector EyeLocation;
FRotator EyeRotation;
End = EyeLocation + (EyeRotation.Vector() * 1000);
MyOwner->GetActorEyesViewPoint(EyeLocation, EyeRotation);
bool bBlockingHit = GetWorld()->LineTraceSingleByObjectType(Hit, EyeLocation, End, objectQueryParams);//返回值是代表射线是否碰到某个物体
AActor* HitActor = Hit.GetActor();
if (HitActor) {
if (HitActor->Implements
APawn* MyPawn = Cast
ISGameplayInterface::Execute_Interact(HitActor, MyPawn);
}
}
FColor LineColor = bBlockingHit ? FColor::Green : FColor::Red;//如果碰到了射线检测就是绿色,没碰到就是红色
DrawDebugLine(GetWorld(), EyeLocation, End, FColor::Red, false, 2.0f, 0, 2.0f);//头文件是#include "DrawDebugHelpers.h",函数作用是展示射线检测的线条