• U++ 人物基本移动、跳跃以及攻击实现,射线检测,cast to


    Character.h:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "GameFramework/SpringArmComponent.h"
    4. #include "Camera/CameraComponent.h"
    5. #include "CoreMinimal.h"
    6. #include "GameFramework/Character.h"
    7. #include "SCharacter.generated.h"
    8. UCLASS()
    9. class SPECIAL_API ASCharacter : public ACharacter
    10. {
    11. GENERATED_BODY()
    12. public:
    13. // Sets default values for this character's properties
    14. ASCharacter();
    15. protected:
    16. // Called when the game starts or when spawned
    17. virtual void BeginPlay() override;
    18. UPROPERTY(VisibleAnywhere)
    19. UCameraComponent* CameraComp;
    20. UPROPERTY(VisibleAnywhere)
    21. USpringArmComponent* SpringArmComp;
    22. UPROPERTY(EditAnywhere)
    23. TSubclassOf ProjectileClass;
    24. public:
    25. // Called every frame
    26. virtual void Tick(float DeltaTime) override;
    27. // Called to bind functionality to input
    28. virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
    29. void MoveForward(float Value);
    30. void MoveRight(float Value);
    31. void PrimaryAttack();
    32. };

    Character.cpp:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "SCharacter.h"
    3. #include "GameFramework/characterMovementComponent.h"
    4. // Sets default values
    5. ASCharacter::ASCharacter()
    6. {
    7. // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
    8. PrimaryActorTick.bCanEverTick = true;
    9. CameraComp = CreateDefaultSubobject("Camera");
    10. SpringArmComp = CreateDefaultSubobject("SpringArm");
    11. SpringArmComp->SetupAttachment(RootComponent);
    12. CameraComp->SetupAttachment(SpringArmComp);
    13. SpringArmComp->SetRelativeLocationAndRotation(FVector(30.0f, 0.0f, 20.0f), FRotator(-30.0f, 0.0f, 0.0f));
    14. CameraComp->bUsePawnControlRotation = true;
    15. this->bUseControllerRotationYaw = false;
    16. GetCharacterMovement()->bOrientRotationToMovement = true;
    17. }
    18. // Called when the game starts or when spawned
    19. void ASCharacter::BeginPlay()
    20. {
    21. Super::BeginPlay();
    22. }
    23. // Called every frame
    24. void ASCharacter::Tick(float DeltaTime)
    25. {
    26. Super::Tick(DeltaTime);
    27. }
    28. void ASCharacter::MoveForward(float Value) {
    29. FRotator ControlRot = GetControlRotation();
    30. ControlRot.Pitch = 0.0f;
    31. ControlRot.Roll = 0.0f;
    32. AddMovementInput(ControlRot.Vector(), Value);
    33. return;
    34. }
    35. void ASCharacter::MoveRight(float Value) {
    36. FRotator ControlRot = GetControlRotation();
    37. ControlRot.Pitch = 0.0f;
    38. ControlRot.Roll = 0.0f;
    39. FVector RightVector = FRotationMatrix(ControlRot).GetScaledAxis(EAxis::Y);
    40. AddMovementInput(RightVector, Value);
    41. return;
    42. }
    43. void ASCharacter::PrimaryAttack()
    44. {
    45. FVector HandLocation = GetMesh()->GetSocketLocation("spine_03");
    46. FTransform SpawnTM = FTransform(GetControlRotation(), HandLocation);
    47. FActorSpawnParameters SpawnParams;
    48. SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
    49. GetWorld()->SpawnActor(ProjectileClass, SpawnTM, SpawnParams);
    50. return;
    51. }
    52. // Called to bind functionality to input
    53. void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    54. {
    55. Super::SetupPlayerInputComponent(PlayerInputComponent);
    56. PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
    57. PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
    58. PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
    59. PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
    60. PlayerInputComponent->BindAction("PrimaryAttack", IE_Pressed, this, &ASCharacter::PrimaryAttack);
    61. PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
    62. }

    Bullet.h:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "GameFramework/Actor.h"
    5. #include "SMagicProjectile.generated.h"
    6. class USphereComponent;
    7. class UProjectileMovementComponent;
    8. class UParticleSystemComponent;
    9. UCLASS()
    10. class SPECIAL_API ASMagicProjectile : public AActor
    11. {
    12. GENERATED_BODY()
    13. public:
    14. // Sets default values for this actor's properties
    15. ASMagicProjectile();
    16. protected:
    17. UPROPERTY(VisibleAnywhere)
    18. USphereComponent* SphereComp;
    19. UPROPERTY(VisibleAnywhere)
    20. UProjectileMovementComponent* MovementComp;
    21. UPROPERTY(VisibleAnywhere)
    22. UParticleSystemComponent* EffectComp;
    23. // Called when the game starts or when spawned
    24. virtual void BeginPlay() override;
    25. public:
    26. // Called every frame
    27. virtual void Tick(float DeltaTime) override;
    28. void DestroyMyself();
    29. };

    Bullet.cpp:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "SMagicProjectile.h"
    3. #include "GameFramework/ProjectileMovementComponent.h"
    4. #include "particles/ParticleSystemComponent.h"
    5. #include "Components/SphereComponent.h"
    6. // Sets default values
    7. ASMagicProjectile::ASMagicProjectile()
    8. {
    9. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
    10. PrimaryActorTick.bCanEverTick = true;
    11. SphereComp = CreateDefaultSubobject("SphereComp");
    12. RootComponent = SphereComp;
    13. EffectComp = CreateDefaultSubobject("EffectComp");
    14. EffectComp->SetupAttachment(SphereComp);
    15. MovementComp = CreateDefaultSubobject("MoveComp");
    16. MovementComp->InitialSpeed = 0.0f;
    17. MovementComp->bRotationFollowsVelocity = true;
    18. MovementComp->bInitialVelocityInLocalSpace = true;
    19. MovementComp->ProjectileGravityScale = 0;
    20. }
    21. // Called when the game starts or when spawned
    22. void ASMagicProjectile::BeginPlay()
    23. {
    24. Super::BeginPlay();
    25. FTimerHandle Handle;
    26. GetWorld()->GetTimerManager().SetTimer(Handle, this, &ASMagicProjectile::DestroyMyself, 3.0f, false);
    27. }
    28. // Called every frame
    29. void ASMagicProjectile::Tick(float DeltaTime)
    30. {
    31. Super::Tick(DeltaTime);
    32. }
    33. void ASMagicProjectile::DestroyMyself()
    34. {
    35. this->Destroy();
    36. }

    cast to:

    APawn* MyPawn = Cast(MyOwner);、

    射线检测:

    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(MyOwner);
                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",函数作用是展示射线检测的线条

  • 相关阅读:
    网球运动目标检测跟踪
    Deepin UOS Linux 编译安装升级 python3
    高效畅通的iOS平台S5配置指南
    DolphinScheduler 集群部署
    Elsevier出版社 | 优质好刊合集
    【 C++ 】map、multimap的介绍和使用
    elasticsearch查询之keyword字段的查询打分控制
    【C++】map和set
    【网络是怎样连接的】第六章 请求到达服务器以及响应给客户端(完结)
    宽字节注入
  • 原文地址:https://blog.csdn.net/qqQQqsadfj/article/details/126450295