【UE4】First Person Shooter C++ Tutorial心得

First Person Shooter C++ Tutorial【原文地址】是Unreal官方WiKi上的一个C++案例,按照它的步骤做了一遍,对于UnrealEngine4的C++编程入门还是很有帮助的。

我使用的引擎版本是4.10,WiKi上使用的版本更新到4.6,实际操作上没什么区别,按照它的具体步骤来就行。为了操作简单,所有的命名都照抄案例即可。4.10比4.6方便的地方是,4.10已经不用关闭编辑器进行编译了。因此,案例中要求你关闭编辑器进行编译的步骤你可以无视掉。

在引擎中添加的C++源代码文件,不管是pawn,actor,hud,character等等类型,最终都会出现在“Games-项目名-Source-项目名”这个目录下面。它会同时生成你命名的同名的h文件和cpp文件。你所创建的类,都是继承自引擎已经封装好的C++类,例如,你创建的character类就是继承自ACharacter。

class DEMO_API AMyCharacter : public ACharacter 

//class 项目名_API A+类名:public ACharacter

首先,就是按键映射问题。在编辑器的INPUT标签下,UE4提供了两种按键映射,一种是Action Mappings,一种是Axis Mappings。对应的是离散的按键输入和连续型的按键输入。比较好理解,人物前后左右移动的按键输入是连续性的,跳跃射击则是离散型的。

在编辑器中设置好按键后,需要在我们的C++代码中实现按键的响应操作:

	InputComponent->BindAxis("look_up", this, &AMyCharacter::AddControllerPitchInput);
	InputComponent->BindAction("jump", IE_Pressed, this, &AMyCharacter::OnStartJump);
所有的按键设置按照上面这两行照做就行了,AxisName/ActionName对应的是你在编辑器设置的按键映射的名称,这里字母名称大小写不区分。

<span style="white-space:pre">	</span>InputComponent->BindAction("jump", IE_Pressed, this, &AMyCharacter::OnStartJump);
	InputComponent->BindAction("jump", IE_Released, this, &AMyCharacter::OnStopJump); 
Action映射的特殊点:IE_Pressed/IE_Released,这两个枚举类型对应0和1。

简单介绍下向前行走的代码吧,左右移动的代码是类似的。区别在于左右移动的代码少了将pitch设置为0的步骤。

void AFPSCharacter::MoveForward(float Value) //float Value,Value就是你在在编辑器设置的值
    {
        if ( (Controller != NULL) && (Value != 0.0f) )
        {
            // find out which way is forward(找到前进的方向)
            FRotator Rotation = Controller->GetControlRotation(); //得到Controller的旋转结构体
            // Limit pitch when walking or falling
            if (GetCharacterMovement()->IsMovingOnGround() || GetCharacterMovement()->IsFalling() )
            {
                Rotation.Pitch = 0.0f;
            }                         //角色处于平地上移动状态或者下落状态时,pitch设置为0,pitch注释如下:
<span style="white-space:pre">	</span>/** Rotation around the right axis (around Y axis), Looking up and down (0=Straight Ahead, +Up, -Down) */
            // add movement in that direction
            const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X); //获得前进方向的向量
            AddMovementInput(Direction, Value); //角色在Direction方向上移动Value个单位
        }
    }

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值