Technology Sharing

UEC Unreal 5 Third Person Shooter Game (Part 2)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

UEC++ Unreal 5 Third Person Shooter Game (Part 2)

Derivative Grenade Weapons

  • Create a new one that inherits fromWeaponAs a subclass of the derived grenade weapon
    insert image description here
  • WillWeaponIn the classFireFunction AdditionvirtualThe keyword becomes a virtual function so that the grenade class can inherit and rewrite it
    insert image description here
  • existProjectileWeaponRewriteFireFunction, create a new template variable for generating projectiles
    insert image description here
  • FireFunction rewriting logic
    insert image description here
  • Code
	//生成的投射物
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category = "ProjectileWeapon")
	TSubclassOf<AActor> ProjectileClass;
//--------------------------------------------------------------------------------------------------
void AProjectileWeapon::Fire()
{
   
	AActor* MyOwner = GetOwner();
	if (MyOwner)
	{
   
		FVector EyeLocation;
		FRotator EyeRotation;
		MyOwner->GetActorEyesViewPoint(EyeLocation, EyeRotation);

		FActorSpawnParameters SpawnParams;
		//设置投射物生成参数:即使生成位置有碰撞,也要强行生成投射物,不进行碰撞检测处理
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		//获取枪口位置
		FVector MuzzleLocation = SkeletalComponent->GetSocketLocation(MuzzleSocketName);
		//生成并发射投射物
		GetWorld()->SpawnActor<AActor>(ProjectileClass, MuzzleLocation, EyeRotation, SpawnParams);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Derived grenade weapons

  • Create a blueprint for the derived grenade class and add the mesh model to it
    insert image description here
  • Change the name of the slot in the mesh model to ourC++The name defined in
    insert image description here
  • CreateActorThe blueprint is used as a bullet, and a launcher and a sphere mesh are added. The initial and maximum speeds of the launcher are set to 2000, and the projectile rebound effect is turned on. The sphere mesh turns on physical simulation.
    insert image description here
    insert image description here
  • Added grenade weapon blueprint spawn projectiles exposed in blueprint spawn bullets to it
    insert image description here
  • In the character blueprint, change the instance's weapon type to a howitzer weapon, and then change the instance's object to the parent weapon. This will make it easier for us to test the problem when calling the function.
    insert image description here
  • Add an explosion event to the bullet blueprint and write the logic
    insert image description here
  • existBeginPlayCall the logic event
    insert image description here
  • operation result
    Please add a description of the image

Sniper's zoom boost effect

  • Create four new variables in the character class: one for turning on zoom push, one for the zoom field of view, one for getting the default field of view, and one for the rate of linear interpolation to the new view.
    insert image description here
  • Create a right-click operation input
    insert image description here
  • Create a new right-click operation processing function
    insert image description here
  • Assign new perspectives for movement and interpolation rates for movement
    insert image description here
  • existBeginPlayGet the default viewing angle range from
    insert image description here
  • Right-click to bind function logic and set new perspective linear interpolation range logic
    insert image description here
  • Binding Input Operations
    insert image description here
  • Create a new operation input in the engine and add it to the mapping
    insert image description here
    insert image description here
  • Bind operations to the character blueprint
    insert image description here
  • operation result
    Please add a description of the image

Creating a console variable

  • Create a static variable in the weapon class as a variable to control the console, control whether to shoot and draw lines, and create a new automatic management console variable class
    insert image description here
  • FAutoConsoleVariableRef: A class for automatically managing console variables
FAutoConsoleVariableRef ACVRDebugWeaponDrawLine(TEXT("COOP.DebugWeapons"), DebugWeaponDrawLine,TEXT