4空pawn unreal
2016-09-02 15:08:37 sgnyyy 阅读数 1893

Actor 相当于 Unity中的Gameobject

Pawn相当于带了控制器或者被AI控制的继承于Actor的类。 

2017-11-22 14:24:30 WAN_EXE 阅读数 643

Pawn这个单词不会翻译,找不到可以形象描述这个单词的汉语,大概的意思就是可以通过很多的Components合成一个含有多种属性状态的物体。

这里主要是如果获取键盘和鼠标的输入,

在Edit  -- Project Setting -- Engine -- Input里面控制,有两种属性,一种是Action Mappings,另外一种是Axis Mapping.

这两种是有区别的,Action主要针对不是连续的,比如跳,射击等,Axis是连续的,比如移动,旋转等。

贴上代码

MyPawn.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

UCLASS()
class UNREALLEARN_API AMyPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AMyPawn();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	
	UPROPERTY(EditAnywhere)
	USceneComponent* OurVisibleComponent;

	//Input functions
	void Move_XAxis(float AxisValue);
	void Move_YAxis(float AxisValue);
	void StartGrowing();
	void StopGrowing();

	//Input variables
	FVector CurrentVelocity;
	bool bGrowing;
};

MyPawn.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.h"
#include "Camera/CameraComponent.h"

// Sets default values
AMyPawn::AMyPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Set this pawn to be controlled by the lowest-numbered player
	AutoPossessPlayer = EAutoReceiveInput::Player0;

	// Create a dummy root component we can attach things to.
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
	// Create a camera and a visible object
	UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
	OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
	// Attach our camera and visible object to our root component. Offset and rotate the camera.
	OurCamera->SetupAttachment(RootComponent);
	OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
	OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
	OurVisibleComponent->SetupAttachment(RootComponent);

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	// Handle growing and shrinking based on our "Grow" action
    {
        float CurrentScale = OurVisibleComponent->GetComponentScale().X;
        if (bGrowing)
        {
            // Grow to double size over the course of one second
            CurrentScale += DeltaTime;
        }
        else
        {
            // Shrink half as fast as we grow
            CurrentScale -= (DeltaTime * 0.5f);
        }
        // Make sure we never drop below our starting size, or increase past double size.
        CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
        OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
    }

    // Handle movement based on our "MoveX" and "MoveY" axes
    {
        if (!CurrentVelocity.IsZero())
        {
            FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
            SetActorLocation(NewLocation);
        }
    }
}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	// Respond when our "Grow" key is pressed or released.
    InputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);
    InputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing);

    // Respond every frame to the values of our two movement axes, "MoveX" and "MoveY".
    InputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
    InputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
}

void AMyPawn::Move_XAxis(float AxisValue)
{
    // Move at 100 units per second forward or backward
    CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::Move_YAxis(float AxisValue)
{
    // Move at 100 units per second right or left
    CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

void AMyPawn::StartGrowing()
{
    bGrowing = true;
}

void AMyPawn::StopGrowing()
{
    bGrowing = false;
}


2015-10-27 19:40:30 pizi0475 阅读数 987

http://blog.csdn.net/neil3d/article/details/46723589


Unreal Engine 4发布好长好长时间了,直到最近才有时间仔细去看一下。

TimSweeney老大一句话“IF YOU LOVE SOMETHING, SET IT FREE”,原来需要几十万授权金才能拿到的东西,就从$19,变成免费了,而且开源。作为一个国际顶尖的引擎,能走出这一步,对我等普通开发者真是福音啊。如此的牛X,再加上开源,相信Unreal Engine 4会火起来的,你看看最近E3上的产品展示也能感觉到。不过,Unreal的定位毕竟是“国际顶尖”,而非Unity3D那样的“开发大众化”,所以上手是有一定难度的。

下面我把对Unreal Engine 4的初步印象总结一下,特别针对像我一样有Unreal Engine 3的同学,希望更多的同学来已经学习Unreal。


UnrealScript去掉了

开发语言上最大的变化,莫过于把UnrealScript去掉了。
UnrealScript吧,当年来看还是一个非常NB的东西,要知道Unreal一代发布是1998年的事儿,而JAVA语言也就是95年才发布的。据说Tim Sweeney在开始设计Unreal的时候曾经考虑过使用Java作为上层语言,不过那时候Java还不成熟,所以他参考Java,实现了这个一个面向对象的,单根继承的脚本语言。不过,随着时间流转,Epic似乎并没有花太大的力气去演进UnrealScript。在Unreal Engine 3的时代,它确实显得有点老旧了,书写起来比一些现代语言确实有很多不方便的地方。所以,去掉就去掉吧。不过,值得注意的是,官方也留出了整合其他脚本语言的接口,例如LUA。


C++11

底层很多基础代码看上去有很强的亲切感,底层架构设计思路沿用了许多。底层依然是使用C++,不过用了很多C++11的特性,代码看上去简洁了不少。
项目的编译、构建使用UnrealBuildTool,这应该是从3代延续过来;另外,就是增加了一个UnrealHeaderTool工具,猜想是根据UCLASS,UPROPERTY等宏,生成自定义反射信息用的,具体的还待进一步研究。


Blueprint Visual Scripting

据说这是UE4最牛X的改进了。看了看,原来是Kismet的延伸,连源代码很多都是UKismentXXX那一套。UE3里面的Kistmet只限于在一个关卡中使用,而Blueprint扩展了。关卡可以有唯一的一个Level Blueprint,相当于原来的Kismet;另外增加了Blueprint Class,大概就是用Blueprint创建自定义的Actor、Pown之类的,由于有了这个功能所以原来的Archetype顺带也就被替代了。其实,作为老一代屌丝Coder,我一直对Kismet那种表达式层级可视化编程,一直不太感冒(像Unity的PlayMaker那样,提供更高层级抽象的可视化工具更好)。不过,既然是UE4主推的个东东,还是得看看。
不过,总体上给Designer一套可视化编程的东西,让他们自己实现一些关卡逻辑、游戏规则之类的,还真是一个特别好的方法。当然,我们这些Coder的工作还是不会丢掉的,例如游戏框架,游戏一些底层功能、常用模块还是要C++写好(或者使用LUA脚本?),封装给Blueprint来使用的。


AnimTree哪去了

UE3的AnimTree给我震撼太大了,所以特别关心UE4的动画系统。看了一下,貌似被分解成了BlendSpace和AnimGraph。
  • BlendSpace
    好比说“站立、走、跑”这三个动作,在UE3的AnimTree里面是有一个特定的node来混合的,根据移动速度不同。在UE4里,则需要创建一个BlendSpace1D资源,然后暴露出Speed参数。
  • AnimBlueprint
    使用Blueprint,AnimGraph,状态机等等控制角色的动画,怎么看上去和Unity的Mecanim有点像呢,唉~
看来AnimTree是真的不见了,很遗憾,因为我觉得那个使用树形结构来抽象的动画系统,实在是非常清晰而且强大。

渲染系统

基于物理的渲染(PBR:Physically-Based Rendering)效果真的是太NB了,Unity5虽然也是PBR,好像比UE4还是略逊一筹啊!这个无需多言了,各种DEMO视频大家都看了不少了。渲染流程也完全走延迟渲染了。但多线程渲染,SceneProxy、Material之类的基础架构没怎么变。

Behavior Tree

这个东西好像在国外的游戏AI领域这几年挺流行了,是个很高大上的东西,UE4直接做了,太好了。

另外,还有很多重大改进,例如Package,资源导入,增加插件支持等,这里就不一一细说了。推荐看一下官网的文章吧,作为本文的补充偷笑

2017-06-23 13:16:36 maya_00 阅读数 267
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "MyPawn.generated.h"

UCLASS()
class ZWEIPROJEKT_API AMyPawn : public APawn
{
    GENERATED_BODY()

private:
    float XScaleIncrement = 0.0;
    float YScaleIncrement = 0.0;
    float Distance = 0.0;
    float ZPosition = 0.0;
    bool RightButtonPressed = false;
    bool MiddleBtnPressed = false;

public:
    // Sets default values for this pawn's properties
    AMyPawn();

    UPROPERTY(VisibleAnywhere)
    USpringArmComponent* CameraArm;

    UPROPERTY(VisibleAnywhere)
    UCameraComponent* Camera;

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
    void XAxis(float Scale);
    void YAxis(float Scale);
    void MiddleButtonAxis(float MiddleButtonAxis);
    void RightMouseButtonPressed();
    void RightMouseButtonReleased();
    void MiddleButtonPressed();
    void MiddleButtonReleased();

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};




----------------------------------------------------------------------------------------------------------------------------------------------



// Fill out your copyright notice in the Description page of Project Settings.

#include "MyPawn.h"
#include "Kismet/KismetMathLibrary.h"


// Sets default values
AMyPawn::AMyPawn()
{
     // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    CameraArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArm"));
    RootComponent = CameraArm;

    Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
    Camera->SetupAttachment(CameraArm);

    CameraArm->bDoCollisionTest = 0;

    FVector WorldOriginalCenter = FVector::ZeroVector;
    SetActorLocation(WorldOriginalCenter);
    Distance = CameraArm->TargetArmLength;
    CameraArm->CameraRotationLagSpeed = 10.0;
    CameraArm->bEnableCameraRotationLag = 1;
}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
    PlayerInputComponent->BindAxis("XAxis",this,&AMyPawn::XAxis);
    PlayerInputComponent->BindAxis("YAxis", this, &AMyPawn::YAxis);
    PlayerInputComponent->BindAxis("MiddleButtonAxis", this, &AMyPawn::MiddleButtonAxis);

    PlayerInputComponent->BindAction("RightMouseButton", IE_Pressed,this, &AMyPawn::RightMouseButtonPressed);
    PlayerInputComponent->BindAction("RightMouseButton", IE_Released, this, &AMyPawn::RightMouseButtonReleased);
    PlayerInputComponent->BindAction("MiddleButtonPressed", IE_Pressed, this, &AMyPawn::MiddleButtonPressed);
    PlayerInputComponent->BindAction("MiddleButtonPressed", IE_Released, this, &AMyPawn::MiddleButtonReleased);

}

void AMyPawn::XAxis(float Scale) {
    if (RightButtonPressed)
    {
        FRotator CameraRotation = GetActorRotation();
        XScaleIncrement += Scale;
        FRotator ChangeRotation(CameraRotation.Pitch, XScaleIncrement, CameraRotation.Roll);
        SetActorRotation(ChangeRotation);
    }
}

void AMyPawn::YAxis(float Scale) {
    if (RightButtonPressed)
    {
        FRotator CameraRotation = GetActorRotation();
        YScaleIncrement -= Scale;
        YScaleIncrement = UKismetMathLibrary::Clamp(YScaleIncrement ,-80,80);
        FRotator ChangeRotation(YScaleIncrement, CameraRotation.Yaw, CameraRotation.Roll);
        SetActorRotation(ChangeRotation);
    }

    if (MiddleBtnPressed)
    {
        FVector CameraLocation = GetActorLocation();
        ZPosition = CameraLocation.Z;
        ZPosition += Scale;
        FVector ChangeLocation = FVector::ZeroVector;
        ChangeLocation.Z = ZPosition;
        SetActorLocation(ChangeLocation);
    }
}

void AMyPawn::MiddleButtonAxis(float Scale) {
    Distance += Scale;
    CameraArm->TargetArmLength = Distance;
}

void AMyPawn::RightMouseButtonPressed() {
    RightButtonPressed = true;
}

void AMyPawn::RightMouseButtonReleased() {
    RightButtonPressed = false;
}

void AMyPawn::MiddleButtonPressed() {
    MiddleBtnPressed = true;
}

void AMyPawn::MiddleButtonReleased() {
    MiddleBtnPressed = false;
}




Unreal Engine 4 发布

阅读数 787

没有更多推荐了,返回首页