UE4日常笔记 UE4C++笔记 增加基础移动(要是character类才能这么做,蹲的输出)
.h
protected:
void MoveForward(float value);
void MoveRight(float value);
void BeginCrouch();
void EndCrouch();
.cpp
#include"Components/InputComponent.h"
#include"GameFramework/PawnMovementCompoent.h"
AGameCharacter::AGameCharacter()
{
GetMovementComponent()->GetNavAgentPropertiesRef().bCanCrouch=true;
//在编辑窗口设置cancrouch为真
}void AGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("jump", IE_Pressed, this, &AGameCharacter::Jump);
//UE4自带了JUMP函数
PlayerInputComponent->BindAction("Crouch",IE_Pressed,this,&AGameCharacter::BeginCrouch);
PlayerInputComponent->BindAction("Crouch", IE_Released, this, &AGameCharacter::EndCrouch);
PlayerInputComponent->BindAxis("MoveForward",this,&AGameCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight",this,&AGameCharacter::MoveRight);
PlayerInputComponent->BindAxis("CameraX",this,&AGameCharacter::AddControllerPitchInput);
//UE4自带了XYZ的控制输出
PlayerInputComponent->BindAxis("CameraY",this,&AGameCharacter::AddControllerYawInput);
}
void AGameCharacter::MoveForward(float value)
{
AddMovementInput(GetActorForwardVector()*value);
}void AGameCharacter::MoveRight(float value)
{
AddMovementInput(GetActorRightVector()*value);
}void AGameCharacter::BeginCrouch()
{
Crouch();
//movement自带有的函数
}void AGameCharacter::EndCrouch()
{
UnCrouch();
//movement自带有的函数
}
增加粒子的方法
,h
.h
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "weapon")//UPROPERTY是包裹属性,EditDefaultsOnly是告诉程序这稚嫩在默认菜单上面编辑,blueprintreadonly告诉我们蓝图只能读取,category对其分类
class UParticleSystem*MuzzleEffect;
【UE4笔记(_杰森大师)】.cpp
.cpp
#include"Particles/ParticleSystem.h"
FVector MuzzleLocation = MeshComponent->GetSocketLocation(MuzzleSocketName);
//SapwnEmitterAtLocation需要获取位置自己定义一个
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TraceEffect, MuzzleLocation);
UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, MeshComponent, MuzzleSocketName);
//meshComponent和muzzlesocketname都是自己定义的
线性检测
.cpp
AActor*MyOwner=GetOwner();
//蓝图spawn函数里面有个owner持有者用主角self就可以使用
FVector Eyelocation;
//发射位置
FVector EyeRotation;
//发射角度
MyOwner->GetActorEyesViewPoint(Eyelocation,EyeRotation);
//(这函数是获取actor双眼视角)获取发射位置和角度
if(GetWorld()->LineTraceSingleByChannel(Hit,Eyelocation,traceEnd,ECC_Visibility/*这个是检测的通道时可见的*/,QueryParams))//先定义相关的值,在这里进行调用
{
//在这里输入被射线检测到时输出的事件
}