How to charge an Arrow

~(sparkplug)~
Sat, 14 May 2016 05:50:03 +0000
World Creation

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!

9 posts - 1 participant