Demo entry 6746041

c++

   

Submitted by anonymous on May 28, 2018 at 08:57
Language: C++. Code size: 5.0 kB.

    #include "tp.h"      
    #include "Kismet/HeadMountedDisplayFunctionLibrary.h"      
    #include "tpCharacter.h"      
    //////////////////////////////////////////////////////////////////////////      
    // AtpCharacter      
    AtpCharacter::AtpCharacter()      
    {      
        //碰撞的设定尺寸      
        GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);      
          
        //设置输入入速率      
        BaseTurnRate = 45.f;      
        BaseLookUpRate = 45.f;      
          
        // 当控制器旋转时,不要旋转模型。让它只控制相机旋转。      
        bUseControllerRotationPitch = false;      
        bUseControllerRotationYaw = false;      
        bUseControllerRotationRoll = false;      
          
        // 配置角色的移动      
        GetCharacterMovement()->bOrientRotationToMovement = true; // 沿输入方向移动 GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...保持在这个旋转速率 GetCharacterMovement()->JumpZVelocity = 600.f;      
        GetCharacterMovement()->AirControl = 0.2f;      
          
        //创建一个相机组件(如果发生碰撞,向玩家拉)      
        CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));      
        CameraBoom->SetupAttachment(RootComponent);      
        CameraBoom->TargetArmLength = 300.0f; // 相机在这个跟随。          
        CameraBoom->bUsePawnControlRotation = true; //控制器控制手臂转动      
          
        //创建相机跟随      
        FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));      
        FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // 将相机连接到指向的末端,让基准线调整以匹配控制器的方向。  FollowCamera->bUsePawnControlRotation = false; //相机不会相对于手臂旋转。      
        // 网格网格和ANIMBlueprints引用网格组件(从字符继承)      
        // 避免在C++中直接引用的内容      
    }      
          
    //////////////////////////////////////////////////////////////////////////      
    // Input      
          
    void AtpCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)      
    {      
        // 设置游戏按键绑定      
        check(PlayerInputComponent);      
        PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);      
        PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);      
          
        PlayerInputComponent->BindAxis("MoveForward", this, &AtpCharacter::MoveForward);      
        PlayerInputComponent->BindAxis("MoveRight", this, &AtpCharacter::MoveRight);      
        PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);      
        PlayerInputComponent->BindAxis("TurnRate", this, &AtpCharacter::TurnAtRate);      
        PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);      
        PlayerInputComponent->BindAxis("LookUpRate", this, &AtpCharacter::LookUpAtRate);      
          
              
    }      
          
          
    void AtpCharacter::OnResetVR()      
    {      
        UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();      
    }      
          
    void AtpCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)      
    {      
            Jump();      
    }      
          
    void AtpCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)      
    {      
            StopJumping();      
    }      
          
    void AtpCharacter::TurnAtRate(float Rate)      
    {      
        //从速率信息计算这个帧的增量      
        AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());      
    }      
          
    void AtpCharacter::LookUpAtRate(float Rate)      
    {      
        // 从速率信息计算这个帧的增量      
        AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());      
    }      
          
    void AtpCharacter::MoveForward(float Value)      
    {      
        if ((Controller != NULL) && (Value != 0.0f))      
        {      
            // 确定正方向        const FRotator Rotation = Controller->GetControlRotation();      
            const FRotator YawRotation(0, Rotation.Yaw, 0);      
          
            //获得正向矢量      
            const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);      
            AddMovementInput(Direction, Value);      
        }      
    }      
          
    void AtpCharacter::MoveRight(float Value)      
    {      
        if ( (Controller != NULL) && (Value != 0.0f) )      
        {      
            // 确定右方向      
            const FRotator Rotation = Controller->GetControlRotation();      
            const FRotator YawRotation(0, Rotation.Yaw, 0);      
              
            // 获得右向矢量       
            const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);      
            //添加运动到角色      
            AddMovementInput(Direction, Value);      
        }      
    }     

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).