Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "BulletBase.h"
  5. #include "GameFramework/ProjectileMovementComponent.h"
  6. #include "Components/StaticMeshComponent.h"
  7. #include "Components/SphereComponent.h"
  8. #include "WeaponBase.h"
  9. #include "Engine/EngineTypes.h"
  10. #include "Kismet/GameplayStatics.h"
  11. #include "GameFramework/DamageType.h"
  12.  
  13. // Sets default values
  14. ABulletBase::ABulletBase()
  15. {
  16. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  17. PrimaryActorTick.bCanEverTick = true;
  18.  
  19. HitSphere = CreateDefaultSubobject<USphereComponent>(TEXT("HitSphere"));
  20. SetRootComponent(HitSphere);
  21.  
  22. HitSphere->InitSphereRadius(12.0);
  23. HitSphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
  24. HitSphere->SetCollisionProfileName(TEXT("BlockAllDynamic"));
  25. HitSphere->SetNotifyRigidBodyCollision(true);
  26.  
  27.  
  28. MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
  29. MeshComp->SetupAttachment(HitSphere);
  30. MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  31.  
  32. if (GetOwner())
  33. {
  34. MeshComp->IgnoreActorWhenMoving(GetOwner(), true);
  35. }
  36.  
  37. if (GetOwner() && GetOwner()->GetOwner())
  38. {
  39. MeshComp->IgnoreActorWhenMoving(GetOwner()->GetOwner(), true);
  40. }
  41.  
  42.  
  43. ProjectileMovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
  44. ProjectileMovementComp->InitialSpeed = 3000;
  45. ProjectileMovementComp->Velocity = FVector(1.0, 0.0, 0.0);
  46.  
  47. }
  48.  
  49. // Called when the game starts or when spawned
  50. void ABulletBase::BeginPlay()
  51. {
  52. Super::BeginPlay();
  53.  
  54. HitSphere->OnComponentHit.AddDynamic(this, &ABulletBase::OnHit);
  55.  
  56. }
  57.  
  58. // Called every frame
  59. void ABulletBase::Tick(float DeltaTime)
  60. {
  61. Super::Tick(DeltaTime);
  62.  
  63. }
  64.  
  65. void ABulletBase::OnHit(UPrimitiveComponent* OnComponentHit, UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
  66. {
  67. if (!OtherActor)
  68. {
  69. return;
  70. }
  71.  
  72. AWeaponBase* MyGun = Cast<AWeaponBase>(GetOwner());
  73. float DamageToDeal;
  74.  
  75. if (MyGun)
  76. {
  77. DamageToDeal = MyGun->Damage;
  78. }
  79. else
  80. {
  81. DamageToDeal = 50;
  82. }
  83.  
  84. float DamageMultiplier = 1.0;
  85. if (Hit.BoneName == TEXT("Head"))
  86. {
  87. float DamageMultiplier = 2.0;
  88. }
  89.  
  90. const float FinalDamage = DamageToDeal * DamageMultiplier;
  91.  
  92. UGameplayStatics::ApplyDamage(OtherActor, FinalDamage, nullptr, GetOwner(), UDamageType::StaticClass());
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement