控制`Actor`朝向,运动 Learn Unreal Engine (with C++)

9 篇文章 1 订阅

控制Actor的朝向,以及Actor的运动

SpaceshipBattle · fanxingin/UE4项目 - 码云 - 开源中国 (gitee.com)

控制Actor朝向鼠标

  1. 设置鼠标在游戏中可见

    • 获取玩家控制器
    • 鼠标可见设置为true
    PC = Cast<APlayerController>(GetController());
    PC->bShowMouseCursor = true;
    
  2. 获取鼠标与主角之间的角度

    • 获取角度
    • 设置角度
    void ASpaceShip::LookAtCursor()
    {
    	FVector MouseLocation;
    	FVector MouseDirection;
    	//获取鼠标位置
    	PC->DeprojectMousePositionToWorld(MouseLocation,MouseDirection);
    	//获取当前位置到目标位置所需要旋转的角度
    	//只在XY平面旋转
    	FVector TargetLoaction = FVector(MouseLocation.X, MouseLocation.Y, GetActorLocation().Z);
    	FRotator Rotator = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), TargetLoaction);
    	SetActorRotation(Rotator);
    }
    
  3. 这样就使主角始终朝向鼠标

控制Actor运动

  1. 在设置中映射活动与按键

    • 连续运动选择轴映射, 例如:连续的前进
    • 不连续的运动选择操作映射,例如:跳跃,拾取,开火

    image-20220108235032147

  2. 输入运动

    • 相当于将输入先存储
    • 让后将存储的向量设置给Actor
    • 然后再Tick()中调用Move()
    void ASpaceShip::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    {
    	Super::SetupPlayerInputComponent(PlayerInputComponent);
    	PlayerInputComponent->BindAxis("MoveUp", this, &ASpaceShip::MoveUp);
    	PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceShip::MoveRight);
    }
    
    void ASpaceShip::MoveUp(float Value)
    {
    	//MovementInput<--------------------(方向)*(值)
    	//Movementinput---->ConsumeMovementInputVector
    	AddMovementInput(FVector::ForwardVector,Value);
    }
    
    void ASpaceShip::Move(float time)
    {
    	//给对象加向量        获取移动向量          *time为了防止速度过大穿越  检测碰撞           
    	AddActorWorldOffset(ConsumeMovementInputVector()* Speed * time, true);
    	/*也可以使用这种方法获取时间 需要头文件
    	 *FApp::GetDeltaTime()
    	 */
    }
    

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值