最近战舰世界(World of Warships)在11月27号公测,舰娘厨估计都很兴奋吧.这次在它公测之前用UE4和C++写一个小的战舰世界的demo,算是对它致敬吧
先放出WOWS的官网http://wows.kongzhong.com/
然后放一张舰队的图
正文来了.
新建一个C++的工程,叫WarOfWarShip,如图
新建一个空白的关卡
然后去找UE4内置给我的地形材质,发现有一个Ocean Water的,如图.我们就使用这个材质
水还是很良心的
然后回到刚刚的主场景,导入一艘我们的主角船.我这次选的模型是雪風号驱逐舰.
运行一下看 效果还是挺良心的
不过还是少了点什么,船没有随着水波飘动.刷一个梗 <来自风平浪静的明天>,那我们开始写C++代码,让传动起来
新建C++类
选择StaticMeshActor类,为我们的静态传模型添加代码
然后等VS启动
加上些代码 处理在水面漂浮
// MyWarShip.h
// Write By 小小酥 on 2015-11-21
#pragma once
#include "Engine/StaticMeshActor.h"
#include "MyWarShip.generated.h"
/**
*
*/
UCLASS()
class WAROFWARSHIP_API AMyWarShip : public AStaticMeshActor
{
GENERATED_BODY()
public:
AMyWarShip();
virtual void Tick(float DeltaTime) override;
virtual void BeginPlay() override;
void PostInitProperties(FPropertyChangedEvent & PropertyChangedEvent);
UPROPERTY(EditAnywhere, Category = "MyWarShip")
float WaveStrength;
UPROPERTY(VisibleAnywhere, Category = "MyWarShip")
FVector2D WaveFactor;
FVector2D WaveOffset;
#if WITH_EDITOR
virtual void PostInitProperties() override;
#endif
};
//MyWarShip.cpp
#include "WarOfWarShip.h"
#include "MyWarShip.h"
AMyWarShip::AMyWarShip() {
WaveStrength = 1.5f;
WaveFactor = FVector2D::ZeroVector;
WaveOffset.X = FMath::FRandRange(0, WaveStrength * 10);
WaveOffset.Y = FMath::FRandRange(0, WaveStrength * 10);
PrimaryActorTick.bCanEverTick = true;
}
void AMyWarShip::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
float delta = DeltaTime*WaveStrength;
WaveFactor.X *= delta * 0.73f;
WaveFactor.Y *= delta * 1.22f;
FRotator rot(0, 0, 0);
rot.Pitch = FMath::Sin(WaveOffset.X + WaveFactor.X)*WaveStrength*0.75f;
rot.Roll = FMath::Sin(WaveOffset.Y + WaveFactor.Y)*WaveStrength*1.5f;
SetActorRelativeRotation(rot);
//SetActorRotation(rot);
}
void AMyWarShip::BeginPlay() {
Super::BeginPlay();
SetMobility(EComponentMobility::Movable);
}
#if WITH_EDITOR
void AMyWarShip::PostInitProperties() {
Super::PostInitProperties();
WaveFactor.X = FMath::FRandRange(0, WaveStrength * 10);
WaveFactor.Y = FMath::FRandRange(0, WaveStrength * 10);
}
#endif
最后的效果