本文主要是介绍ue4 打中物体,物体销毁,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如何让子弹或者一个物体发射出去之后让打中的物体消失呢?
在这里修改ue4的第一人称射击设计项目的模板。让子弹打中的物体消失
修改项目 .h 文件
改为
打开人物蓝图【.h】文件,添加一个方法
private:void RayCast();
右键该函数,选择【QuickActorSAndRefactorings】–>【Create Declaration/DeFinition】实现它
找到人物蓝图的【.cpp】文件。找到刚才实现的方法
void ARCCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{//InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &ARCCharacter::TouchStarted);if (EnableTouchscreenMovement(PlayerInputComponent) == false){//注意修改这边的一个函数。调用Onfire函数变为执行RayCast// 原来的 PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ARCCharacter::OnFire);PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ARCCharacter::RayCast);}
}
然后开始编写函数
void ARCCharacter::RayCast()
{//新建一个hitresultFHitResult * HitResult = new FHitResult();//获得起始点FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();//获得forwardVectorFVector ForWardVector = FirstPersonCameraComponent->GetForwardVector();//设置最终点FVector EndTrace = (ForWardVector*2000.f) + StartTrace;// Collision Query Params 碰撞查询参数FCollisionQueryParams* CQP = new FCollisionQueryParams();//判断发出射线是否碰撞if (GetWorld()->LineTraceSingleByChannel(*HitResult,StartTrace,EndTrace,ECC_Visibility, *CQP)){//如果有碰撞,就做一条射线DrawDebugLine(GetWorld(),StartTrace,EndTrace,FColor(255,0,0),true);//判断是否打中Actorif (HitResult->GetActor()!=NULL){//物体销毁HitResult->GetActor()->Destroy();}}}
忘记了很早以前参考哪位大神的了,在此谢过大神
这篇关于ue4 打中物体,物体销毁的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!