ue c++ 枪的子弹射向屏幕准星位置(射线检测)

void AShooterCharacter::FireWeapon()
{    
 

    if (FireSound) 
    {
        UGameplayStatics::PlaySound2D(this,FireSound);
    }
    const USkeletalMeshSocket* BarrelSocket = GetMesh()->GetSocketByName("BarrelSocket");

    if (BarrelSocket) 
    {
        const FTransform SocketTransform = BarrelSocket->GetSocketTransform(GetMesh());
    
        if (MuzzleFlash) 
        {
            UGameplayStatics::SpawnEmitterAtLocation(GetWorld(),MuzzleFlash,SocketTransform);
        }
        FVector BeamEnd;
        //在这里创建bBeamEnd变量,即调用了一次GetBeamEndLocation函数
        bool bBeamEnd = GetBeamEndLocation(SocketTransform.GetLocation(),BeamEnd);
        if (bBeamEnd) 
        {
            if (ImpactParticles) 
            {
                UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, BeamEnd);
            }
            UParticleSystemComponent* Beam = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(),BeamParticles,SocketTransform);

            if (Beam) 
            {
                Beam->SetVectorParameter(FName("Targert"), BeamEnd);
            }
        }

    }
    UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();

    if (AnimInstance && HipFireMontage)
    {
        AnimInstance->Montage_Play(HipFireMontage);
        AnimInstance->Montage_JumpToSection(FName("StartFire"));
    }
}


bool AShooterCharacter::GetBeamEndLocation(const FVector& MuzzleSocketLocation, FVector& OutBeamLocation) {
    //获取屏幕大小
    FVector2D ViewportSize;

    if (GEngine && GEngine->GameViewport)
    {
        GEngine->GameViewport->GetViewportSize(ViewportSize);
    }
    //用变量CrosshairLocation获取准星在屏幕的位置
    FVector2D CrosshairLocation(ViewportSize.X / 2.f, ViewportSize.Y / 2.f);
    CrosshairLocation.Y -= 50.F;
    FVector CrosshairWorldPosition;
    FVector CrosshairWorldDirection;

    //调用DeprojectScreenToWorld方法来获取准星所在的 世界 位置和方向
    bool bScreenToWorld = UGameplayStatics::DeprojectScreenToWorld(UGameplayStatics::GetPlayerController(this, 0), CrosshairLocation, CrosshairWorldPosition, CrosshairWorldDirection);

    if (bScreenToWorld)
    {
        FHitResult ScreenTraceHit;
        const FVector Start{ CrosshairWorldPosition };
        const FVector End{ CrosshairWorldPosition + CrosshairWorldDirection * 50'000.f };

        OutBeamLocation = End;

        GetWorld()->LineTraceSingleByChannel(ScreenTraceHit, Start, End, ECollisionChannel::ECC_Visibility);

        //上面进行第一次射线检测,判断枪口到准星的位置生成的射线是否有 被阻挡
        if (ScreenTraceHit.bBlockingHit)
        {
            OutBeamLocation = ScreenTraceHit.Location;

        }
        FHitResult WeaponTraceHit;
        const FVector WeaponTraceStart{ MuzzleSocketLocation };
        const FVector WeaponTraceEnd{ OutBeamLocation };
        GetWorld()->LineTraceSingleByChannel(WeaponTraceHit, WeaponTraceStart, WeaponTraceEnd, ECollisionChannel::ECC_Visibility);
        if (WeaponTraceHit.bBlockingHit)
        {
            OutBeamLocation = WeaponTraceHit.Location;
        }
        UE_LOG(LogTemp, Warning, TEXT("Fire Weapon"));
        return true;
    }

    return false;
}

UE5 C++中实现发射子弹的过程下[^1]: 1. 创建子弹类:首先,需要创建一个子弹类,可以继承自UE4的AActor类。在子弹类中,可以定义子弹的属性和行为,例如速度、伤害等。 *** 3. 射击逻辑:在类中,实现射击逻辑。当玩家按下射击按钮时,创建一个子弹实例,并设置其初始位置和速度。可以使用UE4提供的函数来控制子弹的运动。 4. 碰撞检测:为了让子弹能够与其他物体发生碰撞,需要在子弹类中添加碰撞组件,并设置碰撞通道。可以使用UE4提供的碰撞检测函数来检测子弹与其他物体的碰撞。 5. 爆炸效果:当子弹与物体发生碰撞时,可以在碰撞事件中创建一个爆炸效果,并播放相应的特效和声音。可以使用UE4提供的粒子系统和音频组件来实现爆炸效果。 6. 子弹销毁:当子弹与物体发生碰撞后,需要将子弹销毁,以释放资源和减少性能开销。可以使用UE4提供的函数来销毁子弹实例。 下面是一个示例代码,演示了如何在UE5 C++中实现发射子弹的过程: ```cpp // 子弹类 class ABullet : public AActor { // 子弹属性和行为的定义 }; // 类 class AGun : public AActor { // 属性和行为的定义 // 射击逻辑 void Shoot() { // 创建子弹实例 ABullet* Bullet = GetWorld()->SpawnActor<ABullet>(BulletClass, GetActorLocation(), GetActorRotation()); // 设置子弹的初始位置和速度 Bullet->SetActorLocationAndRotation(GetMuzzleLocation(), GetMuzzleRotation()); Bullet->SetVelocity(GetMuzzleDirection() * BulletSpeed); } // 碰撞检测 void OnBulletHit(ABullet* Bullet, AActor* OtherActor) { // 判断碰撞的物体是否是发射子弹的人(自己) if (OtherActor != GetOwner()) { // 创建爆炸效果 UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ExplosionEffect, Bullet->GetActorLocation()); UGameplayStatics::PlaySoundAtLocation(GetWorld(), ExplosionSound, Bullet->GetActorLocation()); // 销毁子弹 Bullet->Destroy(); } } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值