请选择 进入手机版 | 继续访问电脑版

01BIM社区

 找回密码
 立即注册

扫一扫,访问微社区

查看: 6842|回复: 2

官方案例《Player Input and Pawns》的错误

[复制链接]

155

主题

643

帖子

2531

积分

金牌会员

Rank: 6Rank: 6

积分
2531
发表于 2018-3-24 23:20:56 | 显示全部楼层 |阅读模式
官方案例《Player Input and Pawns》的错误
13.jpg
应为:UStaticMeshComponent*  OurVisibleComponent;
回复

使用道具 举报

155

主题

643

帖子

2531

积分

金牌会员

Rank: 6Rank: 6

积分
2531
 楼主| 发表于 2018-3-24 23:36:12 | 显示全部楼层
14.jpg
回复 支持 反对

使用道具 举报

155

主题

643

帖子

2531

积分

金牌会员

Rank: 6Rank: 6

积分
2531
 楼主| 发表于 2018-3-25 07:12:21 | 显示全部楼层
本帖最后由 tzbm123456 于 2018-3-25 07:18 编辑

正确Player Input and Pawns原文:
一、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 HOWTO_PLAYERINPUT_API AMyPawn : public APawn
{
        GENERATED_BODY()
public:
        // Sets default values for this pawn's properties
        AMyPawn();
protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;
public:        
        // Called every frame
        virtual void Tick(float DeltaTime) override;
        // Called to bind functionality to input
        virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

        UPROPERTY(EditAnywhere)
                //USceneComponent* OurVisibleComponent;
                UStaticMeshComponent* OurVisibleComponent;
        void Move_XAxis(float AxisValue);
        void Move_YAxis(float AxisValue);
        void StartGrowing();
        void StopGrowing();

        FVector CurrentVelocity;
        bool bGrowing;
};
二、MyPawn.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPawn.h"
#include "HowTo_PlayerInput.h"
#include "Runtime/Engine/Classes/Camera/CameraComponent.h"
// Sets default values
AMyPawn::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;
        AutoPossessPlayer = EAutoReceiveInput:layer0;
        // Create a dummy root component we can attach things to.
        RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
        // Create a camera and a visible object
        UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
        //OurVisibleComponent = CreateDefaultSubobject<USceneComponent>(TEXT("OurVisibleComponent"));
        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 spawned
void AMyPawn::BeginPlay()
{
        Super::BeginPlay();        
}
// Called every frame
void 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 input
void 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, -2.0f, 2.0f) * 100.0f;
}
void AMyPawn::Move_YAxis(float AxisValue)
{
        // Move at 100 units per second right or left
        CurrentVelocity.Y = FMath::Clamp(AxisValue, -2.0f, 2.0f) * 100.0f;
}
void AMyPawn::StartGrowing()
{
        bGrowing = true;
}
void AMyPawn::StopGrowing()
{
        bGrowing = false;
}
三、Input配置
15.jpg


回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|01BIM社区 - 最专业的BIM技术交流平台 ( 渝ICP备15000873号 )

GMT+8, 2024-3-29 16:46 , Processed in 0.058026 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表