Unreal教学(6)——玩家输入和Pawn(Player Input and Pawns)
来源:互联网 发布:软件竣工总结报告 编辑:程序博客网 时间:2024/06/05 04:11
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 propertiesAMyPawn();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;UPROPERTY(EditAnywhere)USceneComponent* OurVisibleComponent;//Input functionsvoid Move_XAxis(float AxisValue);void Move_YAxis(float AxisValue);void StartGrowing();void StopGrowing();//Input variablesFVector 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 valuesAMyPawn::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 playerAutoPossessPlayer = EAutoReceiveInput::Player0;// Create a dummy root component we can attach things to.RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));// Create a camera and a visible objectUCameraComponent* 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 spawnedvoid AMyPawn::BeginPlay(){Super::BeginPlay();}// Called every framevoid 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 inputvoid 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;}
阅读全文
0 0
- Unreal教学(6)——玩家输入和Pawn(Player Input and Pawns)
- Unreal教学(7)——控制玩家相机(Player-Controlled Cameras)
- Unreal教学(12)——增加玩家的能力(Enhancing Player Abilities)
- Unreal教学(4)——组件和碰撞(Components and Collision)
- Unreal教学(1)——序言
- UE4学习笔记2nd:玩家输入和Pawns
- Unreal教学(5)——变量,时间,事件(Variables, Timers, and Events)
- Unreal教学(10)——放置Actor (Placing Actors)
- Unreal教学(9)——虚幻引擎名词解释(Unreal Engine 4 Terminology)
- Unreal教学(2)——快速开发程序(Programming Quick Start)
- Unreal教学(3)——游戏相机控制(Game-Controlled Cameras)
- Unreal教学(8)——蓝图学习快速开始(Blueprints Quick Start Guide)
- Unreal教学(11)——物体交互(Object Interaction with Blueprints)
- Unreal教学(13)——创建界面(Creating Screen UI Elements)
- Building JavaScript Games for Phones Tablets and Desktop(6)- 响应玩家输入
- Codeforces 382D Ksenia and Pawns(逆向dfs)
- 【Unet】Unet 产生不同玩家( Spawning different player prefabs)
- Fill-rate,Canvases and input 填充率,画布和输入—(Unity3d)
- c#快递单打印精灵
- 扔掉家里不必要的东西,为新物品腾出空间
- Gitlab 10.1.4 (汉化版) for CentOS 7.4安装
- 如何求旋转矩阵
- Redis故障案例(一)-特定key批量丢失
- Unreal教学(6)——玩家输入和Pawn(Player Input and Pawns)
- maven 打包时mapper.xml打不进去问题
- 沃尔沃把自动驾驶卡车从瑞典带到798转了个圈【附视频】
- STM32,RTC周期性唤醒(wakeup)中断
- web语义化
- 怎么安装苹果系统mac os x虚拟机
- Uber将购2.4万辆沃尔沃 建无人车队 巴黎拟向共享单车征税
- matlab之dlmwrite换行写入txt
- js如何判断一个对象{}是否为空对象,没有任何属性