Actor范围内随机生成 Learn Unreal Engine (with C++)

2022/1/10 17:11:55

本文主要是介绍Actor范围内随机生成 Learn Unreal Engine (with C++),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Actor范围内随机生成 Learn Unreal Engine (with C++)

SpaceshipBattle · fanxingin/UE4项目 - 码云 - 开源中国 (gitee.com)

Actor范围内随机生成

  1. 新建box组件

    SpawnArea = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnArea"));
    	RootComponent = SpawnArea;
    
  2. 获取随机生成位置

    FVector AEnemySpawner::GetGenerateLocation()
    {
    	float Distance = 0;
    	FVector Location;
    	
    	while (Distance< MinimumDistanceToPlayer)
    	{
    		//在盒子中产生的随机的点
    		Location = UKismetMathLibrary::RandomPointInBoundingBox(SpawnArea->Bounds.Origin, SpawnArea->Bounds.BoxExtent);
    		Distance = (Location - SpaceShip->GetActorLocation()).Size();
    	}
    	return Location;
    }
    
  3. 在指定位置生成Actor

    FActorSpawnParameters SpawnParameters;
    		//           生成敌人
    		GetWorld()->SpawnActor<AEnemy>(Enemy, GetGenerateLocation(), FRotator::ZeroRotator, SpawnParameters);
    

在这里插入图片描述



这篇关于Actor范围内随机生成 Learn Unreal Engine (with C++)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程