Ue5 websocket控制Character前后左右动作

# myue521Character.h

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once


#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "myue521Character.generated.h"

UCLASS(config=Game)
class Amyue521Character : public ACharacter
{
	 
public:

	void NotifyServer();
	virtual void myJump();

public: 
	FString Str = TEXT("walawala");
	FString topicLogKey = TEXT("log");
	FString walkLogKey = TEXT("walk");
	FString Forward = TEXT("Forward");
	FString Right = TEXT("Right");
	void TriggeredBySocketMsg(FString jsonString);

	void doMyForward(int f);// 1或者-1

	void doMyRight(int r);// 1或者-1
};

# myue521Character.cpp

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#include "myue521Character.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "UObject/ConstructorHelpers.h"  
#include "UWebSocketGameInstance.h"
//
// Amyue521Character


void Amyue521Character::myJump()
{
	bPressedJump = true;
	JumpKeyHoldTime = 0.0f;

	this->NotifyServer();


} 

void Amyue521Character::Look(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D LookAxisVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// add yaw and pitch input to controller
		AddControllerYawInput(LookAxisVector.X);
		AddControllerPitchInput(LookAxisVector.Y);
	}
}

void Amyue521Character::NotifyServer() {

	//从Character里获取UGameInstance的例子
	UUWebSocketGameInstance* GameInstance = Cast<UUWebSocketGameInstance>(GetGameInstance());
	if (GameInstance) {
		if (GameInstance->WebSocket->IsConnected()) {
			GameInstance->WebSocket->Send("FROM UnrealEngine 5");
		}
	}
} 

void Amyue521Character::TriggeredBySocketMsg(FString jsonString)
{
	
	//UE_LOG(LogTemp, Warning, TEXT("%s 事件触发调用了我的函数CallBackFunMul。%s-%s"), *FString(__FUNCTION__), *(this->Str), *jsonString);
	//GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, FString::Printf(TEXT("事件触发调用了我的函数CallBackFunMul,%s-%s"), *(this->Str), *jsonString));

	TSharedRef< TJsonReader<> > Reader = TJsonReaderFactory<>::Create(jsonString);
	TSharedPtr<FJsonObject> Root;

	if (FJsonSerializer::Deserialize(Reader, Root))
	{
		if (Root->HasField(TEXT("Topic"))) {
			FString Topic = Root->GetStringField(TEXT("Topic"));
			TSharedPtr<FJsonObject, ESPMode::ThreadSafe> Data = Root->GetObjectField(TEXT("Data"));

			// TSharedPtr<FJsonObject, ESPMode::ThreadSafe> ObjectField = Object->GetObjectField(TEXT("realtime"));
			FString KeyField = Data->GetStringField("Key");
			FString ValueField = Data->GetStringField("Value");

			// log类型打印log
			if (this->topicLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
				GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, ValueField);
			}else if (this->walkLogKey.Equals(KeyField, ESearchCase::IgnoreCase)) {
				FString DirectionField = Data->GetStringField("Direction");
				if (this->Forward.Equals(DirectionField, ESearchCase::IgnoreCase)) {
					this->doMyForward(FCString::Atoi(*ValueField));

				}else if (this->Right.Equals(Right, ESearchCase::IgnoreCase)) {
					this->doMyRight(FCString::Atoi(*ValueField));
				}

			}
			else {
				GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "1-Key: " + KeyField + ",Value:" + ValueField);
			}

		}
		else {
			GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "解析json异常");
		}
	}
}

void Amyue521Character::doMyForward(int f) // 1或者-1
{
	FVector2D MovementVector = FInputActionValue::Axis2D(0, f);

	 
	if (Controller != nullptr)
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

		// get right vector 
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

		// add movement 
		AddMovementInput(ForwardDirection, MovementVector.Y);
		AddMovementInput(RightDirection, MovementVector.X);
	}
}

void Amyue521Character::doMyRight(int r)// 1或者-1
{
	FVector2D MovementVector = FInputActionValue::Axis2D(r, 0);


	if (Controller != nullptr)
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

		// get right vector 
		const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

		// add movement 
		AddMovementInput(ForwardDirection, MovementVector.Y);
		AddMovementInput(RightDirection, MovementVector.X);
	}
}
相关推荐
序属秋秋秋1 小时前
算法基础_数据结构【单链表 + 双链表 + 栈 + 队列 + 单调栈 + 单调队列】
c语言·数据结构·c++·算法
apcipot_rain2 小时前
【密码学——基础理论与应用】李子臣编著 第五章 序列密码 课后习题
算法·密码学
不要不开心了2 小时前
sparkcore编程算子
pytorch·分布式·算法·pygame
88号技师2 小时前
【2024年最新IEEE Trans】模糊斜率熵Fuzzy Slope entropy及5种多尺度,应用于状态识别、故障诊断!
人工智能·算法·matlab·时序分析·故障诊断·信息熵·特征提取
清同趣科研2 小时前
R绘图|6种NMDS(非度量多维分析)绘图保姆级模板——NMDS从原理到绘图,看师兄这篇教程就够了
人工智能·算法
杜小暑3 小时前
冒泡排序与回调函数——qsort
c语言·算法·排序算法
徵6863 小时前
代码训练day27贪心算法p1
算法·贪心算法
AgilityBaby3 小时前
UE5蓝图设置界面尺寸大小
ue5·游戏引擎
Nigori7_4 小时前
day32-动态规划__509. 斐波那契数__70. 爬楼梯__746. 使用最小花费爬楼梯
算法·动态规划
x_feng_x4 小时前
数据结构与算法 - 数据结构与算法进阶
数据结构·python·算法