Aiming and Crosshairs - 3

2024. 1. 4. 16:19·Unreal 공부/Unreal Engine 4 C++ The Ultimate Shooter

Automatic Fire

연사를 구현할것입니다.

 

ShooterCharacter.h

pubilc:
	void FireButtonPressed();
	void FireButtonReleased();
    
private:
	// Left mouse button or right console trigger pressed
	bool bFireButtonPressed;

	// True when we can fire. False when waiting for the timer
	bool bShouldFire;

	// Fate of Automatic gun fire
	float AutomaticFireRate;

	FTimerHandle AutoFireTimer;

 

void AShooterCharacter::FireButtonPressed()
{
	bFireButtonPressed = true;
}

void AShooterCharacter::FireButtonReleased()
{
	bFireButtonPressed = false;
}

 

그리고 SetupPlaerInputComponent의 FireWeapon을 FireButtonPressed 함수로 대체해주고, FireAction의 Completed도 만들어 줍니다.

void AShooterCharacter::SetupPlayerInputComponent(UInputComponent *PlayerInputComponent)
{
	...
		EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Started, this, &AShooterCharacter::FireButtonPressed);
		EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Completed, this, &AShooterCharacter::FireButtonReleased);
	...
}

 

헤더에서 FireTimer를 다룰 함수를 만들어 줍니다.

	void StartFireTimer();

	UFUNCTION()
	void AutoFireReset();
  • AutoFireReset은 FTimer에 사용되기 때문에 UFUNCTION을 적용해야 합니다.

 

void AShooterCharacter::FireButtonPressed()
{
	bFireButtonPressed = true;
	StartFireTimer();
}

void AShooterCharacter::FireButtonReleased()
{
	bFireButtonPressed = false;
}

void AShooterCharacter::StartFireTimer()
{
	if (bShouldFire)
	{
		FireWeapon();
		bShouldFire = false;
		GetWorldTimerManager().SetTimer(AutoFireTimer, this, &AShooterCharacter::AutoFireReset, AutomaticFireRate);
	}
}

void AShooterCharacter::AutoFireReset()
{
	bShouldFire = true;
	if (bFireButtonPressed)
	{
		StartFireTimer();
	}
}
  1. FireButtonPressed():
    • bFireButtonPressed = true : 발사 버튼이 눌렸음을 나타내는 불리언 변수를 참으로 설정합니다.
    • StartFireTimer() : 발사 타이머를 시작하는 함수를 호출합니다.
  2. FireButtonReleased():
    • bFireButtonPressed = false : 발사 버튼이 놓아졌음을 나타내는 불리언 변수를 거짓으로 설정합니다.
  3. StartFireTimer():
    • if (bShouldFire) { ... } : bShouldFire가 참일 때, 즉 캐릭터가 발사할 준비가 되었을 때 다음의 코드를 실행합니다.
    • FireWeapon() : 무기를 발사하는 함수를 호출합니다.
    • bShouldFire = false : 다음 발사를 위해 bShouldFire를 거짓으로 설정합니다.
    • GetWorldTimerManager().SetTimer(...) : 자동 발사를 위한 타이머를 설정합니다. AutoFireTimer는 타이머의 핸들, this는 현재 클래스의 인스턴스, &AShooterCharacter::AutoFireReset은 타이머가 완료될 때 호출될 함수, AutomaticFireRate는 타이머의 지속 시간입니다.
  4. AutoFireReset():
    • bShouldFire = true : 캐릭터가 다시 발사할 수 있음을 나타냅니다.
    • if (bFireButtonPressed) { StartFireTimer(); } : 발사 버튼이 여전히 눌려있으면 StartFireTimer를 다시 호출하여 연속 발사를 계속합니다.

 

생성자에서 변수의 초기화를 해줍니다.

// Sets default values
AShooterCharacter::AShooterCharacter() : ...
										 // Automatic fire variables
										 AutomaticFireRate(0.1f),
										 bShouldFire(true),
										 bFireButtonPressed(false)

 

 

※ WeaponFire에서 Error가 발생한다면 매개변수를 지워주면됩니다. (원래 쓰지 않던 매개변수)

 

Jumping Animation

 

Juming Animation을 만들기 위해서 ABP_ShooterCharacter에서 새로운 State Machine을 만들것입니다.

 

이후 Idle/Jogs, Jumps, JumpLand를 만들고 전환조건을 각각 맞추어 줍니다.

 

JumpLand에서 Idle/Jogs로 한번더 선을 이으면 2개의 전환조건이 만들어 집니다.

 

그중 새로 생긴 전환에 들어가서  Is In Air을 연결해줍니다. 연결한 이유는 JumpLand가 실행되고 있을때 다시 점프를 할 경우 전환을 위해서 입니다. 

 

각 노드에 맞는 연결을 해줍니다.

기본 애니메이션에 추가적인 애니메이션(Additive Animation)을 적용

 

 

 

Jumps State 안에서 만든 Jumps State Machine을 아래와 같이 구성해줍니다.

 

각자 다른 State에 맞는 애니메이션을 연결해 줍니다.

 

 

이후 ABP_ShooterCharacter > AnimGraph 로 돌아와서 New Save cached pose...로 Cached Locomotion을 만들어 줍니다.

 

Ground Locomotion을 Locomotion으로 대체하고 주석도 교체해줍니다.

 

 

 

'Unreal 공부 > Unreal Engine 4 C++ The Ultimate Shooter' 카테고리의 다른 글

The Weapon - 2  (1) 2024.01.09
The Weapon - 1  (0) 2024.01.05
Aiming and Crosshairs - 2  (1) 2024.01.04
Aiming and Crosshairs - 1  (0) 2023.12.31
Animation - 5  (1) 2023.12.31
'Unreal 공부/Unreal Engine 4 C++ The Ultimate Shooter' 카테고리의 다른 글
  • The Weapon - 2
  • The Weapon - 1
  • Aiming and Crosshairs - 2
  • Aiming and Crosshairs - 1
메카인
메카인
  • 메카인
    메카인의 지식창고
    메카인
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 코딩 공부
        • TIL(Today I Learn)
        • TIL
        • 백준(C++)
        • Python
        • 알고리즘
        • 프로젝트 회고
      • C++
        • C++
        • C++ STL
        • C,C++ mCoding yotube
      • 게임개발
        • 언데드서바이벌_골드메탈_클론코딩
        • 3D_골드메탈_클론코딩
        • 유니티_문제해결
        • 게임 수학
      • Unreal 공부
        • UE5 GameDev
        • Unreal Engine 4 C++ The Ult..
      • 교재 문제 풀이
        • 운영체제
      • 정보처리기사
        • 정처기 요약
        • 정처기 오답노트
      • 학교수업
        • 데이터베이스
        • 프로그래밍 언어론
        • 리눅스 시스템
        • 네트워크
      • 일상
        • 주식
        • 독서
        • 일기
      • (비공개 전용)
        • memory
        • Build
        • OOP
        • Smart Pointer
        • lamda
        • 게임 수학
        • 모던 C++
        • 모던 C++ STL
        • 모던 C++ Concurrency, Paralle..
        • 책
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
    • 블로그 관리
  • 링크

  • 공지사항

    • 공지사항 - 인생과 블로그의 목표
  • 인기 글

  • 태그

    ~
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
메카인
Aiming and Crosshairs - 3
상단으로

티스토리툴바