zoukankan      html  css  js  c++  java
  • [UE4]使用PlayerController获取鼠标点击时的坐标

    1,获取鼠标在当前场景中坐标系统的中的position,加入场景地图的范围是一千平方米,那么这个position的范围也是1000米x1000米。

    注册鼠标事件

    FInputActionBinding &action1 = InputComponent->BindAction("SetDestination", IE_Pressed, this, &AHPlayerController::OnSetDestinationPressed);

    函数实现MoveToMouseCursor(),此函数放在PlayerController::PlayerTick()内调用,重写下PlayerTick():

    void AHPlayerController::MoveToMouseCursor()
    {
        // Trace to see what is under the mouse cursor
        FHitResult Hit;
        GetHitResultUnderCursor(ECC_Visibility, false, Hit);
     
        if (Hit.bBlockingHit)
        {
            // We hit something, move there
            SetNewMoveDestination(Hit.ImpactPoint);
        }
    }

    2,获取鼠标再显示屏内的坐标系统的position。假如屏幕分辨率是1280x720,那么这个position的范围就是(0, 0)到(1280, 720)。PlayerController::GetMousePosition()。

    AHPlayerController* PC = ...
     
    float LocX = 0;
    float LocY = 0;
    PC->GetMousePosition(LocX, LocY);

    3,触屏设备上获取场景内点击的position,其范围与第1种情况相同。

    注册touch事件

    InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AHPlayerController::MoveToTouchLocation);

    函数实现:

    void AHPlayerController::MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location)
    {
        FVector2D ScreenSpaceLocation(Location);
     
        // Trace to see what is under the touch location
        FHitResult HitResult;
        GetHitResultAtScreenPosition(ScreenSpaceLocation, CurrentClickTraceChannel, true, HitResult);
        if (HitResult.bBlockingHit)
        {
            // We hit something, move there
            SetNewMoveDestination(HitResult.ImpactPoint);
        }
    }
  • 相关阅读:
    Linux 下安装JDK1.8
    INSERT IGNORE 与INSERT INTO的区别
    linux安装redis 完整步骤
    从0开始 图论学习 广度优先搜索 链式前向星表示法
    从0开始 图论学习 深度优先遍历 链式前向星表示法
    从0开始 图论学习 链式前向星 最好的建图方法
    从0开始 图论学习 邻接表 STL vector
    从0开始 图论学习 前向星表示法
    数据结构实习
    数据结构实习
  • 原文地址:https://www.cnblogs.com/timy/p/8634227.html
Copyright © 2011-2022 走看看