当前位置: 首页 > wzjs >正文

焦作网站建设价格深圳企业网站备案

焦作网站建设价格,深圳企业网站备案,部署php网站,怎么用ftp修改网站图片最近在学习UE中遇到一些问题,把这些问题记录一下,因为实在废了很大功夫。 在学习了UE5的例子中的第三人称移动Demo,想实现几个NPC在场景内移动。 本来想自己写一个类,遇到一堆问题花费了好几天时间,所以我把问题写下来…

最近在学习UE中遇到一些问题,把这些问题记录一下,因为实在废了很大功夫。

在学习了UE5的例子中的第三人称移动Demo,想实现几个NPC在场景内移动。
本来想自己写一个类,遇到一堆问题花费了好几天时间,所以我把问题写下来方便和我一样初学的同学不要卡住了。

遇到的问题

本来想写一个简单的移动类,发现这些移动可能在上斜坡,或者坐电梯的时候都存在物理问题,采用物理的写法可以上坡,但是在电梯上可能会不跟随电梯,无法达到预期。自己写一个非物理的又很费事。
经过AI咨询一本正经的给出了使用Pawn和UCharacterMovementComponent结合的方式,最后总是报错,AI又开始说用ACharacter,这不是绕了一圈。

上面的错误没绕出来的代码就不贴了(早删掉了) :)

使用ACharacter的问题

后来又采用了ACharacter作为基类制作第三人称类似的来控制NPC移动。
这里其实也卡了很长时间,因为身边实在没人会UE,AI的水平还是不怎么好,GPT,X.ai,深度,豆包等AI都请教了,得到的答复都差不多。

首先我建立好了类AVRPlayerConsole(用来控制npc)这个类基本上和第三人称相同,就是不需要Input部分,创建了蓝图BP_VRPlayerConsole。
我在蓝图里加了一个球体方便观察。
在这里插入图片描述
拖入场景中是这样的
在这里插入图片描述
然后通过一处脚本来创建这个npc。

 加载蓝图类
FString BlueprintPath = TEXT("/Game/BP/BP_VRPlayerConsole.BP_VRPlayerConsole_C");
UClass* BlueprintClass = StaticLoadClass(UObject::StaticClass(), nullptr, *BlueprintPath);if (!BlueprintClass)
{UE_LOG(LogTemp, Error, TEXT("Failed to load blueprint class at %s"), *BlueprintPath);return;
}// 设置生成位置和旋转
FVector SpawnLocation(310.f, 350.f, 300.f);
FRotator SpawnRotation(0.0f, 0.0f, 0.0f);if (BlueprintClass) // 确保蓝图类有效
{charConsole = GetWorld()->SpawnActor<AVRPlayerConsole>(BlueprintClass,SpawnLocation,SpawnRotation,SpawnParams);if (charConsole){UE_LOG(LogTemp, Log, TEXT("成功生成 BP_VRPlayerConsole 实例!"));}
}

当我编译后运行,发现创建出来了,但是他在空中不会随着重力掉下去。

AI解决

AI给的说法一大堆

Movement Mode 是否正确
UpdatedComponent 是否有效
Velocity 是否被更新
是否被 Root Motion 锁定
Tick 是否在执行
强制刷新 Movement
检查是否被其他状态阻止移动
强制启用 Movement Component
检查 Movement Component 是否允许移动
直接修改位置SetActorLocation,验证角色是否能移动
检查 Custom Movement Mode
检查角色的 CanMove() 是否被禁用
尝试强制设置 MovementMode
是否受到物理影响
强制切换 MovementMode
是否禁用了 MovementComponent
确保 charConsole 不是 Physics 角色
运动速度 (MaxWalkSpeed) 是否有效
AddMovementInput 依赖于 RootComponent
charConsole 是否具有控制器 (Controller)

看着就觉得有问题,但是又找不到问题。花费了大把时间研究,都一一排除了。
其实AI提到了控制器,但是初次接触UE的是想不到还有AI控制器等等的说法。

发现

一直觉得是脚本的问题。
后来发现如果把蓝图拖入场景中运行,对象因为重力影响会掉落到地板。当拖入多个也都会落到地板上,只有自己实例化的不行。
再问AI为什么拖入场景的可以落地,自己实例化的不行,又给出实例化要这样要那样,都不对。
最后发现拖入场景的对象运行后会有一个AIControler对象生成。
在这里插入图片描述
再次AI求证才得到了需要创建对象后需要调用

charConsole->SpawnDefaultController(); // 可选:生成AI控制器

这样才可以激活控制器。

就这么简单,需要添加AIController。
就这么一行代码把人折腾死了 。哈哈~~

最后放上这个类的源码做一个记录

源码

.h

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Character.h"#include "VRPlayerConsole.generated.h"UCLASS(config = Game)
class UE5TECHNOLOGY_API AVRPlayerConsole : public ACharacter
{GENERATED_BODY()public:// Sets default values for this character's propertiesAVRPlayerConsole();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	void MoveCharacter(const FVector& Direction);// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.#include "VRPlayerConsole.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Engine/LocalPlayer.h"
// Sets default values
AVRPlayerConsole::AVRPlayerConsole()
{// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = false;// Set size for collision capsuleGetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);// Don't rotate when the controller rotates. Let that just affect the camera.bUseControllerRotationPitch = false;bUseControllerRotationYaw = false;bUseControllerRotationRoll = false;// Configure character movementGetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...	GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint// instead of recompiling to adjust themGetCharacterMovement()->JumpZVelocity = 700.f;GetCharacterMovement()->AirControl = 0.35f;GetCharacterMovement()->MaxWalkSpeed = 500.f;GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;GetCharacterMovement()->BrakingDecelerationFalling = 1500.0f;}// Called when the game starts or when spawned
void AVRPlayerConsole::BeginPlay()
{Super::BeginPlay();//GetCharacterMovement()->SetMovementMode(EMovementMode::MOVE_Walking);//UE_LOG(LogTemp, Warning, TEXT("IsActive: %d"), GetCharacterMovement()->IsActive());}// Called every frame
void AVRPlayerConsole::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}// Called to bind functionality to input
void AVRPlayerConsole::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);}void AVRPlayerConsole::MoveCharacter(const FVector& Direction)
{if (Direction.IsNearlyZero()) return;// 确保 Movement 是激活的UCharacterMovementComponent* MoveComp = GetCharacterMovement();if (MoveComp && MoveComp->IsActive()){MoveComp->AddInputVector(Direction);}else{UE_LOG(LogTemp, Error, TEXT("Character Movement is Invalid!"));}
}

我在我的主玩家控制力添加了一个MoveCharacter函数来测试是否可以移动,直接用主玩家的函数调用过来。

void AVRPlayer::Move(const FInputActionValue& Value)
{// 获取 2D-Axis 值FVector2D MovementVector = Value.Get<FVector2D>();if (MovementVector.IsNearlyZero()){return; // 如果没有输入,就直接返回}if (vrCamera == nullptr){UE_LOG(LogTemp, Log, TEXT("vrCamera null"));return;}// 获取相机的方向FVector ForwardDirection = vrCamera->GetForwardVector(); // 相机的前向向量FVector RightDirection = vrCamera->GetRightVector();     // 相机的右向向量ForwardDirection.Z = 0;// 将 MovementVector 的 X 和 Y 分别映射到相机的方向上FVector MoveDirection = ((ForwardDirection * MovementVector.Y) + (RightDirection * MovementVector.X)).GetSafeNormal();if (charConsole == nullptr){UE_LOG(LogTemp, Log, TEXT("charConsole null !!!"));//UE_LOG(LogTemp, Log, TEXT("VRRoot address: %p"), VRRoot);return;}charConsole->MoveCharacter(MoveDirection);}

最后测试可以传入方向可以移动了。
效果图如下:
请添加图片描述


文章转载自:

http://nhkhTNaN.qfrsm.cn
http://Hp8eMjI5.qfrsm.cn
http://xtQCkGqj.qfrsm.cn
http://SCeGNOdE.qfrsm.cn
http://BMY35ukB.qfrsm.cn
http://Lf1TLWvj.qfrsm.cn
http://788SLlVi.qfrsm.cn
http://ODc6Yh3o.qfrsm.cn
http://PNbhJIQj.qfrsm.cn
http://mZqhkMla.qfrsm.cn
http://CLjwoBFF.qfrsm.cn
http://PeGr83VD.qfrsm.cn
http://0FpDWxfl.qfrsm.cn
http://5WGcfvyJ.qfrsm.cn
http://PPOjfVIc.qfrsm.cn
http://Pi45wKBF.qfrsm.cn
http://w1bKRLZk.qfrsm.cn
http://3EXAV2GF.qfrsm.cn
http://dTkRLcxE.qfrsm.cn
http://eB5ifBr4.qfrsm.cn
http://yXgD0tAb.qfrsm.cn
http://acdFXRkb.qfrsm.cn
http://1yUTeo9v.qfrsm.cn
http://8vsa6Xgg.qfrsm.cn
http://IIKVnErv.qfrsm.cn
http://5t0t5uwe.qfrsm.cn
http://hpDAfYJ3.qfrsm.cn
http://wfOdUMqX.qfrsm.cn
http://KNWUVRah.qfrsm.cn
http://SJnrlEgU.qfrsm.cn
http://www.dtcms.com/wzjs/719439.html

相关文章:

  • 泉州做网站便宜网站建设全网营销
  • 网站建设助手建网站要买服务器吗
  • 一级做A网站wordpress如何采集电影
  • 商城网站续费要多少钱ui设计加班很严重
  • 网站做链接算侵权吗开发微信小程序收费吗
  • 个人网站设计html做这种灰色的网站犯法
  • 常州网站建设外包公司哪家好网络班级网站建设
  • 企业网站制作教程视频wordpress入侵教程
  • 泰州 做网站h5作品欣赏
  • 佛山模板建站企业网站建设公司哪家好
  • 滨江区网站开发公司htm网站的维护
  • 做reference的网站重庆seo整站优化服务
  • 如何建立单页网站域名申请网站
  • 学校网站建设需求分析调研表wordpress主题错误检测
  • 东莞材料网站建设贵州高端网站开发
  • 中国建设监理协会继续教育网站网站制作com cn域名有什么区别
  • 易思网站管理系统收费上海人才网官网站首页
  • 生态农庄网站建设在安徽省住房和城乡建设厅网站
  • 广州建设交易中心网站首页推广软件平台
  • 彩票网站为啥链接做两次跳转最新网络游戏排行榜2021前十名
  • 哪些网站可以发广告wordpress分站点
  • 撰写网站建设策划书范文选服务好的分销管理系统
  • 门户手机版网站网站建设一般步骤
  • 可视化网站开发工具有哪些运营推广的工作内容
  • 苏州市网站为某公司或企业做的门户网站
  • 电子商务网站建设与管理课后题答案6网页设计与网站建设实验报告
  • 上海 宝安网站建设 网络服务360建筑网官方网站
  • 昆山专业做网站wordpress 内网 插件
  • 海南网站建设多少钱贵阳讯玛网站建设
  • 网站源码商城建设做app必须有网站吗