# How to charge an Arrow

**URL:** <https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397>\
**Category:** World Creation\
**Tags:** projectilemovement, unreal-engine, UE4, question, CPP\
**Created:** [May 14, 2016, 5:50am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397 "2016-05-14T05:50:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_sparkplug](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_sparkplug](https://forums.unrealengine.com/u/_sparkplug)\
**Post date:** [May 14, 2016, 5:50am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/1 "2016-05-14T05:50:03Z")

</div>

Hi,

I have been to charge a projectile (StaticMesh) based on how long we hold the LMB.  
But I am literally stuck, I am trying to achieve this using C++.

Below are my codes under

**Character class**

void ABow\_ArrowCharacter::Tick(float DeltaTime)

{

```
Super::Tick(DeltaTime);
{

	if (bHold)
	{
		ChargeTime += DeltaTime;
		if (ChargeTime <= ArrowSpeed)
		{
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Draw"));
			ChargeTime = 0;
		}
	}
}

```

}

void ABow\_ArrowCharacter::Release()

{

```
bHold = false;
USkeletalMeshComponent* UseMesh = GetMesh();
if (ProjectileClass != NULL)
{
	FVector CameraLoc;
	FRotator CameraRot;
	GetActorEyesViewPoint(CameraLoc, CameraRot);
	//FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(GunOffset);
	const FVector SocketLocation = UseMesh->GetSocketLocation("ArrowSocket");
	/*const FRotator SocketRotation = UseMesh->GetSocketRotation("ArrowSocket");*/
	FRotator MuzzleRotation = CameraRot;
	MuzzleRotation.Pitch += 10.0f;
	UWorld* const World = GetWorld();
	if (World)
	{
		FActorSpawnParameters SpawnParams;
		SpawnParams.Owner = this;
		SpawnParams.Instigator = Instigator;
		AProjectile_Arrow* const Projectile = World->SpawnActor<AProjectile_Arrow>(ProjectileClass, SocketLocation, MuzzleRotation, SpawnParams);
		if (Projectile)
		{
			FVector const LaunchDir = MuzzleRotation.Vector();
			Projectile->InitVelocity(LaunchDir);
		}
	}
}

```

}

void ABow\_ArrowCharacter::Draw()

{

```
bHold = true;

```

}

**Projectile Class**

AProjectile\_Arrow::AProjectile\_Arrow()

{

```
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("Collision_comp"));
CollisionComp->InitSphereRadius(5.0f);
CollisionComp->BodyInstance.SetCollisionProfileName("Arrow");
//CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
CollisionComp->OnComponentHit.AddDynamic(this, &AProjectile_Arrow::OnHit);
RootComponent = CollisionComp;

ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
//ProjectileMovement->InitialSpeed = 2000.0f;
//ProjectileMovement->MaxSpeed = 2000.0f;
ProjectileMovement->bRotationFollowsVelocity = true;
ProjectileMovement->ProjectileGravityScale = 0.5f;
ProjectileMovement->bShouldBounce = false;

ArrowMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Arrow"));
ArrowMesh->AttachTo(RootComponent);

InitialLifeSpan = 3.0f;

```

}

Would be great If anyone can shed light on how I can get this done. Thanks a lot in advance.

Cheers!

---

<div class="post-metadata">

**Author:** ![Zarkopafilis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zarkopafilis](https://forums.unrealengine.com/u/Zarkopafilis)\
**Post date:** [May 14, 2016, 6:15am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/2 "2016-05-14T06:15:25Z")

</div>

First, you need to set a way to change the value of the bHold variable. Either by the player clicking again causing  
ABow\_ArrowCharacter::Draw() to be called again, which sould be something like:

```
void ABow_ArrowCharacter::Draw(){ 
    bHold = !bHold;
}

```

Then, on the tick function, you need to calculate the speed of the projectile based on how much time the key was held down. You should clamp the held time though because we dont want our arrows to be able to fly to the moon as soon as the player has charged enough.

```
Super::Tick(DeltaTime);
 {

     if (!bHold && ChargeTime!=0) //shoot the arrow
     {
             float ChargePercent = FMath::Clamp(ChargeTime, 0.0f, MaxChargeTime)/MaxChargeTime;
             float Speed = ChargePercent*MaxArrowSpeed;
             //spawn the projectile here, with the appropriate direction and speed
             ChargeTime = 0;//reset the charge time timer
             bHold = false;
     }else{
             ChargeTime+=DeltaTime;//keep adding charge time, the player does not want to shoot yet
     }
 }

```

Notice the 2 extra variables: MaxArrowSpeed and MaxChargeTime: Max Charge time is the max time the user can charge his bow in order to achieve a speed of MaxArrowSpeed (this is why we clamp).

If you have got issues regarding projectile spawning, may I redirect you to this question as well: [https://answers.unrealengine.com/questions/306257/spawning-a-projectile.html](https://answers.unrealengine.com/questions/306257/spawning-a-projectile.html)

---

<div class="post-metadata">

**Author:** ![\_sparkplug](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_sparkplug](https://forums.unrealengine.com/u/_sparkplug)\
**Post date:** [May 15, 2016, 8:09am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/3 "2016-05-15T08:09:46Z")

</div>

Hi Zark,

Thanks a lot. It makes a lot more sense. But one small doubt, spawning a projectile in a tick will spam the arrows right?  
and I have already got the arrows to travel in the direction I aim in the release function. So should I copy and paste it in the tick function?

I am going to try this tonight.

Thanks,  
Spark

---

<div class="post-metadata">

**Author:** ![Zarkopafilis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zarkopafilis](https://forums.unrealengine.com/u/Zarkopafilis)\
**Post date:** [May 15, 2016, 8:12am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/4 "2016-05-15T08:12:30Z")

</div>

It is not going to spam the arrow because of these 2 lines :  
ChargeTime = 0;//reset the charge time timer  
bHold = false;

It essentially resets the whole mechanism.

Best Regards,  
Zark

---

<div class="post-metadata">

**Author:** ![\_sparkplug](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_sparkplug](https://forums.unrealengine.com/u/_sparkplug)\
**Post date:** [May 17, 2016, 7:24am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/5 "2016-05-17T07:24:35Z")

</div>

HI Zark,

I have been trying this out for the past two days and sorry but as i suspected before the arrows are being spammed not sure if I am doing something wrong.

This what i have so far

void ABow\_ArrowCharacter::Tick(float DeltaTime)

{

```
Super::Tick(DeltaTime);

{

	if (!bHold && ChargeTime != 0)
	{
		float ChargePercent = FMath::Clamp(ChargeTime, 0.0f, MaxChargeTime)/MaxChargeTime;
		float Speed = ChargePercent*MaxArrowSpeed;
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Draw"));
		if (ProjectileClass != NULL)
		{
			FVector CameraLoc;
			FRotator CameraRot;
			GetActorEyesViewPoint(CameraLoc, CameraRot);
			const FVector SocketLocation = GetMesh()->GetSocketLocation("ArrowSocket");
			FRotator MuzzleRotation = CameraRot;
			MuzzleRotation.Pitch += 10.0f;
			UWorld* const World = GetWorld();
			if (World)
			{
				FActorSpawnParameters SpawnParams;
				SpawnParams.Owner = this;
				SpawnParams.Instigator = Instigator;
				AProjectile_Arrow* const Projectile = World->SpawnActor<AProjectile_Arrow>(ProjectileClass, SocketLocation, MuzzleRotation, SpawnParams);
				if (Projectile)
				{
					FVector const LaunchDir = MuzzleRotation.Vector();
					Projectile->InitVelocity(LaunchDir);
				}
			}
		}
		ChargeTime = 0;
		bHold = false;
	}
	else
	{
		ChargeTime += DeltaTime;

	}

}

```

}

void ABow\_ArrowCharacter::Draw()

{

```
bHold = !bHold;

```

}

void ABow\_ArrowCharacter::Release()

{

```
bHold = false;

```

}

Thanks,  
Spark

---

<div class="post-metadata">

**Author:** ![Zarkopafilis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zarkopafilis](https://forums.unrealengine.com/u/Zarkopafilis)\
**Post date:** [May 17, 2016, 8:16am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/6 "2016-05-17T08:16:08Z")

</div>

Since you implemented a Release function, you could revert to having Draw just setting bHold to true. Then, move these parts to the functions accordingly:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/3X/1/4/148572b034b9cc43d01f15facf954a230be391bc.png) ](https://i.imgur.com/rB4Hsen.png)

this should solve all of your problems. You might also want to add a delay before the player is able to re-draw an arrow.

---

<div class="post-metadata">

**Author:** ![\_sparkplug](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_sparkplug](https://forums.unrealengine.com/u/_sparkplug)\
**Post date:** [May 26, 2016, 4:02am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/7 "2016-05-26T04:02:02Z")

</div>

Hi Zark,

I have been trying to do this myself but seems like I am stuck big time. Sorry for being such a pain. I did what you told and the arrow spawns only for the first time and after that it never spawns. So i changed it like this

void ABow\_ArrowCharacter::Release()

{

```
USkeletalMeshComponent* UseMesh = GetMesh();
float DeltaTime = 100;
if (ChargeTime != 0)

{

	float ChargePercent = FMath::Clamp(ChargeTime, 0.0f, MaxChargeTime) / MaxChargeTime;

	float Speed = ChargePercent*MaxArrowSpeed;

	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Draw"));

}

if (ProjectileClass != NULL)

{

		FVector CameraLoc;
		FRotator CameraRot;
		GetActorEyesViewPoint(CameraLoc, CameraRot);
		/*FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(GunOffset);*/
		const FVector SocketLocation = UseMesh->GetSocketLocation("ArrowSocket");
		//const FRotator SocketRotation = UseMesh->GetSocketRotation("ArrowSocket");
		FRotator MuzzleRotation = CameraRot;
		MuzzleRotation.Pitch += 10.0f;
		UWorld* const World = GetWorld();
	if (World)

	{

			FActorSpawnParameters SpawnParams;
			SpawnParams.Owner = this;
			SpawnParams.Instigator = Instigator;
			AProjectile_Arrow* const Projectile = World->SpawnActor<AProjectile_Arrow>(ProjectileClass, SocketLocation, MuzzleRotation, SpawnParams);

		if (Projectile)

		{

				FVector const LaunchDir = MuzzleRotation.Vector();
				Projectile->InitVelocity(LaunchDir);
				Projectile->GetProjectileMovementComponent();

		}
	}
}

ChargeTime = 0;
bHold = false;
if (bHold)
{
	ChargeTime += DeltaTime;
}

```

}

I have added debug message in the IF statement but seems like it is not working. Am I missing something?

thanks,  
Spark

---

<div class="post-metadata">

**Author:** ![Zarkopafilis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zarkopafilis](https://forums.unrealengine.com/u/Zarkopafilis)\
**Post date:** [May 27, 2016, 5:47am UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/8 "2016-05-27T05:47:57Z")

</div>

This is how Release should be: `USkeletalMeshComponent\* UseMesh = GetMesh();  
if (ChargeTime != 0 && bHold)  
{

```
 float ChargePercent = FMath::Clamp(ChargeTime, 0.0f, MaxChargeTime) / MaxChargeTime;

 float Speed = ChargePercent*MaxArrowSpeed;

 GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, TEXT("Shoot Arrow"));

 if (ProjectileClass != NULL)

{

     FVector CameraLoc;
     FRotator CameraRot;
     GetActorEyesViewPoint(CameraLoc, CameraRot);
     /*FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(GunOffset);*/
     const FVector SocketLocation = UseMesh->GetSocketLocation("ArrowSocket");
     //const FRotator SocketRotation = UseMesh->GetSocketRotation("ArrowSocket");
     FRotator MuzzleRotation = CameraRot;
     MuzzleRotation.Pitch += 10.0f;
     UWorld* const World = GetWorld();
 if (World)

 {

         FActorSpawnParameters SpawnParams;
         SpawnParams.Owner = this;
         SpawnParams.Instigator = Instigator;
         AProjectile_Arrow* const Projectile = World->SpawnActor<AProjectile_Arrow>(ProjectileClass, SocketLocation, MuzzleRotation, SpawnParams);

     if (Projectile)

     {

             FVector const LaunchDir = MuzzleRotation.Vector();
             Projectile->InitVelocity(LaunchDir);
             Projectile->GetProjectileMovementComponent();

     }
 }
}

 ChargeTime = 0;

```

bHold = false;

}

And, on tick, if bHold is true, append current delta seconds to chargeTime

---

<div class="post-metadata">

**Author:** ![\_sparkplug](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_sparkplug](https://forums.unrealengine.com/u/_sparkplug)\
**Post date:** [July 21, 2016, 1:29pm UTC](https://forums.unrealengine.com/t/how-to-charge-an-arrow/357397/9 "2016-07-21T13:29:16Z")

</div>

Hi Zark,

Thanks a lot.Sorry I was on vacation. I am getting back to what i was doing. Will let you know how it goes.
