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);
    

你可能感兴趣的:(UE4,C++,虚幻,c++,ue4)