登陆不了wordpress,苏州seo培训,网站迁移后 后台进不去,c2c模式类型一、创建C类 创建武器子弹的类#xff0c;创建生产武器子弹的类#xff0c;创建弹壳的类#xff0c;生产武器子弹的类的父类是武器的类 创建后如图#xff0c;ProjectileMyWeapon类(产生子弹的类)继承自weapon类#xff0c;Projectile(子弹的类)#xff0c;Casing(弹壳声…一、创建C类 创建武器子弹的类创建生产武器子弹的类创建弹壳的类生产武器子弹的类的父类是武器的类 创建后如图ProjectileMyWeapon类(产生子弹的类)继承自weapon类Projectile(子弹的类)Casing(弹壳声音的类) 在子弹的类中添加如下代码
//头文件中添加
private:UPROPERTY(EditAnywhere)class UBoxComponent* CollisionBox;// 碰撞盒的类//构造中添加
CollisionBox CreateDefaultSubobjectUBoxComponent(TEXT(CollisionBox));
SetRootComponent(CollisionBox);
CollisionBox-SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic); //设置自身的碰撞类型
CollisionBox-SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); //启动碰撞启动触发器
CollisionBox-SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore); //设置对其他类型的碰撞
/* 第一个参数对角色 第二个参数是对角色是哪种碰撞 */
CollisionBox-SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Block);
CollisionBox-SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block); 二、武器蒙太奇(开火动画)添加开火功能 1.将动画设置为加性的(让动作连贯)将瞄准和不瞄准的动画找到设置如图 2.创建武器蒙太奇动画在对应动画右键-创建-创建动画蒙太奇 (我将创建好的蒙太奇动画放到了其他文件夹中) 3.新建蒙太奇片段添加插槽将瞄准的动画拖拽到蒙太奇中 将之前的default片段名删除 添加另一端动画 将之前默认跳转的动画清空变成单独的动画 选择插槽 最后样子 三、绑定开火按键 1.编辑-项目设置-输入-操作映射-添加fire鼠标左键 2.在角色类中添加绑定
//角色类头文件
/* 发射子弹函数 */
void FireButtonPressed();
void FireButtonRelease();
/* 发射子弹函数 *///角色类源文件
void ABlasterCharacter::FireButtonPressed()
{if (Combat){Combat-FireButtonPressed(true);}
}void ABlasterCharacter::FireButtonRelease()
{if (Combat){Combat-FireButtonPressed(false);}
}//函数SetupPlayerInputComponent中
PlayerInputComponent-BindAction(Fire, IE_Pressed, this, ABlasterCharacter::FireButtonPressed);
PlayerInputComponent-BindAction(Fire, IE_Released, this, ABlasterCharacter::FireButtonRelease); 3.声明一个蒙太奇动画类指针
//角色头文件
// 当前类指针在界面中赋值 EditAnywhere可编辑 Combat在细节中找到对应设置
UPROPERTY(EditAnywhere , Category Combat)
class UAnimMontage* FireWeaponMontage; // 动画蒙太奇类 4.编译后在角色蓝图中设置对应的蒙太奇动画 5.定义播放动画的函数FName中的名字是蒙太奇动画中的名字
//角色类头文件
void PlayFireMontage(bool bAiming);//角色类源文件
void ABlasterCharacter::PlayFireMontage(bool bAiming)
{if (Combat nullptr || Combat-EquippedWeapon nullptr) return;UAnimInstance* AnimInstance GetMesh()-GetAnimInstance();if (AnimInstance FireWeaponMontage){AnimInstance-Montage_Play(FireWeaponMontage);/* 找到播放哪段动画 名字是动画中新建蒙太奇片段的名字 */FName SectionName;SectionName bAiming ? FName(RifleAim) : FName(RifleHip);/* 找到播放哪段动画 */AnimInstance-Montage_JumpToSection(SectionName);}
} 6.动画蓝图中的改变武器开火实在装备武器后才可以所以如图动画蓝图类中添加 slot中右侧细节可以选择槽 new一个姿势 7.在aimoffset中使用6中的姿势(在动作偏移中使用对应姿势) 8. 在战斗组件类中添加代码(class ABlasterCharacter* Character指针我在角色类中的PostInitializeComponents函数赋值)使用了RPC函数多播功能让动画在每个客户端都能看见
void ABlasterCharacter::PostInitializeComponents()
{Super::PostInitializeComponents();if (Combat){Combat-Character this;}
}
FVector_NetQuantize是FVector的网络传输的序列化的结构减少网络带宽
//战斗组件类头文件
// 开火函数
void FireButtonPressed(bool bPressed);/* Server RPC函数 */
UFUNCTION(Server, Reliable)
void ServerFire(const FVector_NetQuantize TraceHitTarget);
/* Server RPC函数 *//* 多播函数 */
UFUNCTION(NetMulticast , Reliable)
void MulticastFire(const FVector_NetQuantize TraceHitTarget);
/* 多播函数 *//* 命中线 */
void TraceUnderCrosshairs(FHitResult TraceHitResult);
/* 命中线 */class ABlasterCharacter* Character;
bool bFireButtonPressed;//战斗组件类源文件
void UCombatComponent::FireButtonPressed(bool bPressed)
{bFireButtonPressed bPressed;if (bFireButtonPressed){FHitResult HitResult;TraceUnderCrosshairs(HitResult);ServerFire(HitResult.ImpactPoint);}
}void UCombatComponent::TraceUnderCrosshairs(FHitResult TraceHitResult)
{/* 屏幕中心为瞄准点 *//* 获得视口大小 */FVector2D ViewportSize;if (GEngine GEngine-GameViewport){GEngine-GameViewport-GetViewportSize(ViewportSize);}/* 获得屏幕中心坐标 */FVector2D CrosshaurLocation(ViewportSize.X / 2.f, ViewportSize.Y / 2.f);FVector CrosshairWorldPosition; //世界空间中的相应 3D 位置FVector CrosshairWorldDirection; //在给定的 2d 点处远离摄像机的世界空间方向矢量bool bScreenToWorld UGameplayStatics::DeprojectScreenToWorld(UGameplayStatics::GetPlayerController(this, 0),CrosshaurLocation,CrosshairWorldPosition,CrosshairWorldDirection);if (bScreenToWorld){FVector Start CrosshairWorldPosition;TRACE_LENGTH 我设置为8000 在世界坐标的长度可以理解成武器的射程FVector End Start CrosshairWorldDirection * TRACE_LENGTH;/** bool LineTraceSingleByChannel(FHitResult OutHit, // 输出的碰撞信息const FVector Start, // 射线的起点const FVector End, // 射线的终点ECollisionChannel TraceChannel, // 碰撞通道const FCollisionQueryParams Params FCollisionQueryParams::DefaultQueryParam, // 可选的额外查询参数const FCollisionResponseParams ResponseParam FCollisionResponseParams::DefaultResponseParam // 可选的碰撞响应参数);*///检查射线与场景中的物体是否有交点并返回相关的碰撞信息GetWorld()-LineTraceSingleByChannel(TraceHitResult,Start,End,ECollisionChannel::ECC_Visibility);
#if 0if (!TraceHitResult.bBlockingHit){TraceHitResult.ImpactPoint End;HitTarget End;}else{HitTarget TraceHitResult.ImpactPoint;/*DrawDebugSphere(const UWorld* World, // 表示你要在哪个世界中绘制球体FVector Center, // 球体的中心位置float Radius, // 球体的半径int32 Segments, // 球体的分段数影响球体的平滑度FColor Color, // 球体的颜色bool bPersistentLines, // 是否为持久化的调试线条场景切换后是否还存在float LifeTime, // 调试球体的生存时间0 为永久存在uint8 DepthPriority, // 渲染优先级影响是否被遮挡float Thickness // 球体线条的厚度)*/DrawDebugSphere(GetWorld(),TraceHitResult.ImpactPoint,12.f, //半径12, //FColor::Red);}
#endif}
}void UCombatComponent::ServerFire_Implementation(const FVector_NetQuantize TraceHitTarget)
{MulticastFire(TraceHitTarget);
}void UCombatComponent::MulticastFire_Implementation(const FVector_NetQuantize TraceHitTarget)
{if (EquippedWeapon nullptr) return;if (Character){//UE_LOG(LogTemp, Warning, TEXT(FireButtonPressed));Character-PlayFireMontage(bAiming);EquippedWeapon-Fire(TraceHitTarget);}
} 9.武器类添加代码
//武器类头文件
/* 开火功能 */
virtual void Fire(const FVector HitTaget);UPROPERTY(EditAnywhere , Category Weapon Properties)
class UAnimationAsset* FireAnimation; //动画资产类UPROPERTY(EditAnywhere)
TSubclassOfclass ACasing CasingClass; // 监视类 -- 监视蛋壳弹出//武器类源文件
void AWeapon::Fire(const FVector HitTaget)
{if (FireAnimation){WeapomMesh-PlayAnimation(FireAnimation, false);}if (CasingClass){const USkeletalMeshSocket* AmmoEjectSocket WeapomMesh-GetSocketByName(FName(AmmoEject));if (AmmoEjectSocket){FTransform SocketTransform AmmoEjectSocket-GetSocketTransform(GetWeaponMesh());UWorld* World GetWorld();if (World){World-SpawnActorACasing(CasingClass,SocketTransform.GetLocation(),SocketTransform.GetRotation().Rotator());}}}
} 10.创建子弹蓝图类 11.设置蓝图 1.打开蓝图设置对用的武器网格体WeaponMesh细节中网格体的骨骼网格体资产选择对于你武器资产 2.设置pickwidget(没有可以不用设置)细节中用户界面的空间选择屏幕空间类选择对饮蓝图类 3.设置动画(武器开火动画)该蓝图的类是projectileweapon它的父类是weapon父类中有 UPROPERTY(EditAnywhere , Category Weapon Properties) class UAnimationAsset* FireAnimation; //动画资产类 所以一在细节中可以找到(C类的继承) 4.将11中的创建武器蓝图拖拽到地图中 12. 摄像机的偏移(可选若想看见角色前方的可以设置) 13.生产子弹类中代码(GetSocketName中的名字是对应武器网格体中枪口的插槽的名字)
//子弹类头文件
public:virtual void Fire(const FVector HitTaget) override;protected:UPROPERTY(EditAnywhere)TSubclassOfclass AProjectile ProjectileClass;//子弹类源文件
void AProjectileMyWeapon::Fire(const FVector HitTaget)
{Super::Fire(HitTaget);if (!HasAuthority()) return;APawn* InstigatorPawn CastAPawn(GetOwner());const USkeletalMeshSocket* MuzzleFlashSocket GetWeaponMesh()-GetSocketByName(FName(MuzzleFlash));if (MuzzleFlashSocket){FTransform SocketTransform MuzzleFlashSocket-GetSocketTransform(GetWeaponMesh());// 从枪口闪光插座到开火位置 获得尖端的位置FVector ToTarget HitTaget - SocketTransform.GetLocation();FRotator TargetRotation ToTarget.Rotation();if (ProjectileClass InstigatorPawn){FActorSpawnParameters SpawnParams;SpawnParams.Owner GetOwner();SpawnParams.Instigator InstigatorPawn;UWorld* World GetWorld();if (World){World-SpawnActorAProjectile(ProjectileClass,SocketTransform.GetLocation(),TargetRotation,SpawnParams);}}}
} 14.创建子弹类蓝图 15.打开子弹类蓝图设置(中间黄色的是碰撞盒) 15.1 设置碰撞盒大小 16.定义粒子特效类和声音类
//子弹类头文件virtual void Destroyed() override;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UFUNCTION()virtual void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult Hit);public: private:UPROPERTY(VisibleAnywhere)class UProjectileMovementComponent* ProjectileMovementComponent; //子弹运动的类UPROPERTY(EditAnywhere)class UParticleSystem* Tracer; //粒子系统类class UParticleSystemComponent* TracerComponent; //粒子系统组件类UPROPERTY(EditAnywhere)class UParticleSystem* ImpactParticals; //粒子系统类UPROPERTY(EditAnywhere)class USoundCue* ImpactSound;//声音提示类//子弹类源文件
//构造中添加
ProjectileMovementComponent CreateDefaultSubobjectUProjectileMovementComponent(TEXT(ProjectileMovementComponent));
ProjectileMovementComponent-bRotationFollowsVelocity true; //如果为 true则此射弹将在每一帧更新其旋转以匹配其速度方向
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{Super::BeginPlay();if (Tracer){//播放附加到指定组件并跟随指定组件的指定效果。当效果完成时系统将消失。不复制TracerComponent UGameplayStatics::SpawnEmitterAttached(Tracer,//粒子系统创建CollisionBox,//要附加到的组件。FName(),//AttachComponent 中的可选命名点用于生成发射器GetActorLocation(),// 位置 -- 根据 LocationType 的值这是与附加组件/点的相对偏移量或者是将转换为相对偏移量的绝对世界位置如果 LocationType 为 KeepWorldPosition。GetActorRotation(),//旋转 -- 根据 LocationType 的值这是与附加组件/点的相对偏移量或者是将转换为相对偏移量的绝对世界旋转如果 LocationType 为 KeepWorldPositionEAttachLocation::KeepWorldPosition//根据 LocationType 的值这是附加组件中的相对缩放或者是将转换为相对缩放的绝对世界缩放如果 LocationType 为 KeepWorldPosition。//指定 Location 是相对偏移还是绝对世界位置//当粒子系统完成播放时组件是否会自动销毁或者是否可以重新激活//用于池化此组件的方法。默认为 none。//组件是否在创建时自动激活。 );}if (HasAuthority()){CollisionBox-OnComponentHit.AddDynamic(this,AProjectile::OnHit);}
}void AProjectile::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult Hit)
{Destroy();
}void AProjectile::Destroyed()
{Super::Destroyed();if (ImpactParticals){UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticals, GetActorTransform());}if (ImpactSound){UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());}
}17.设置声音和粒子特效设置速度 18. 若想发射子弹时在其他客户端也可以显示在子弹类的构造中将bReplicates true;即可 19.弹壳类
//弹壳头文件UFUNCTION()virtual void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult Hit);#if 0
public: // Called every framevirtual void Tick(float DeltaTime) override;
#endifprivate:UPROPERTY(VisibleAnywhere)UStaticMeshComponent* CasingMesh;//静态网格体类 武器开火时弹出的子弹的网格体UPROPERTY(EditAnywhere)float ShellEjectionImpulse;// 弹壳初速度UPROPERTY(EditAnywhere)class USoundCue* ShellSound;// 弹壳弹出时的声音//弹壳源文件
ACasing::ACasing()
{// Set this actor to call Tick() every frame. You can turn this off to improve performance if you dont need it.PrimaryActorTick.bCanEverTick false;CasingMesh CreateDefaultSubobjectUStaticMeshComponent(TEXT(CasingMesh));SetRootComponent(CasingMesh);CasingMesh-SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera,ECollisionResponse::ECR_Ignore);CasingMesh-SetSimulatePhysics(true); // 物理CasingMesh-SetEnableGravity(true); // 重力CasingMesh-SetNotifyRigidBodyCollision(true); //通知ShellEjectionImpulse 10.f;
}// Called when the game starts or when spawned
void ACasing::BeginPlay()
{Super::BeginPlay();CasingMesh-OnComponentHit.AddDynamic(this, ACasing::OnHit);// 给弹壳添加初始速度CasingMesh-AddImpulse(GetActorForwardVector() * ShellEjectionImpulse);
}
void ACasing::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult Hit)
{if (ShellSound){UGameplayStatics::PlaySoundAtLocation(this, ShellSound, GetActorLocation());}Destroy();
}
#if 0
// Called every frame
void ACasing::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}
#endif20.弹壳类蓝图
文章转载自: http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.zlhcw.cn.gov.cn.zlhcw.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.qcnk.cn.gov.cn.qcnk.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.yzdth.cn.gov.cn.yzdth.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.mtbth.cn.gov.cn.mtbth.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.zyrp.cn.gov.cn.zyrp.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.sqfrg.cn.gov.cn.sqfrg.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn