How can i shoot at the center of my crosshair? C++

Oh bounty is like in stackoverflow, if you have a question that noone answers for more than 2 days, you can offer a “bounty” amount of karma for someone who answers the question.

It would be a great improvement here - since it is very hard to get an answers especially in C++, it could maybe motivate ppl to answer more.

And then the comunity will get happier - because they will get more answers.
A lot of ppl tried UE4 posted questions didnt get any answers and went back to Unity.

I think I will go to a night/morning run, and then will back of to sleep.
for some reason my working hours are messed up in last couple of months

I come here after trying a lot, i have spended almost two days searching and trying different stuffs. I’m so so tired now, and will try tomorrow, anyway, thanks again. Cheers!

Sure, if you will get stuck too
much I will post an example code snippet for you.

It is a great habit to try and solve things by your swlf before asking!
I respect it :slight_smile:

It’s all for the digital karma! :stuck_out_tongue:

As your self-appointed medical professional I am instructing you to get some rest right now :P. But yeah, I’m interested in hearing more about your Bounty System proposal (wouldn’t that be GameDev consulting though?) in any case I have a strange compulsion in trying to reciprocate a small slice of what I think I’ve gained with the IP that Epic has made available for free. (I promise I don’t work for said company (or even in game dev at all) lol).

I’m trying to solve that. (i’m getting some random directions D:)

        UWorld* World = GetWorld();
	FVector2D Viewport;
	World->GetGameViewport()->GetViewportSize(Viewport);

	FVector WorldPosition;
	FVector WorldDirection;
	const FVector StartTrace = CurrentWeapon->WeaponMesh->GetSocketLocation("MF");

	AMyPlayerController* MyPC = Cast<AMyPlayerController>(Controller);
    if(	MyPC->DeprojectScreenPositionToWorld((Viewport.X), (Viewport.Y), WorldPosition, WorldDirection))
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Blue, TEXT("Deprojected!"));



	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;

	FVector Destination = (WorldPosition + WorldDirection) * CurrentWeapon->WeaponConfig.WeaponRange;

	FHitResult Hit = WeaponTrace(WorldPosition, Destination);

	FVector RotNorm = (Destination  - StartTrace);

	if(RotNorm.Normalize())
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Purple, TEXT("Normalized!"));

	FRotator Rot = RotNorm.Rotation();

	AWeaponBullet* Projectile = World->SpawnActor<AWeaponBullet>(ProjectileClass, StartTrace , Rot, SpawnParams);

i’m doing something wrong? (I have some debug things, like lines. I have removed from this anwser)

PHOTOS

PS:

Range = 3000.0f;
“MF” = “Muzzle Flash”

PS2: AHUD Is not working (is not updating my Position and Direction), i did all right, getting and casting my HUD (and I checked if I succeeded on cast), so, Controller is working well.

(I also tried divide by 2 the X an Y viewport) and this Hit.Location()

Hi, sorry I am too tired right now, I just took a quick look and found something:

if( MyPC->DeprojectScreenPositionToWorld((Viewport.X), (Viewport.Y), WorldPosition, WorldDirection))

if you want to get the screen center you have to devide the size by 2 or even better to multiply it by 0.5 :slight_smile:

if( MyPC->DeprojectScreenPositionToWorld((Viewport.X * 0.5f), (Viewport.Y * 0.5f), WorldPosition, WorldDirection))

P.S. for me the deproject works fine I am on 4.16.3 though…

If you can’t get the screen size
maybe you can try this function

const FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());

This is a math problem. if you’re using blueprint, I don’t know if blueprint has said function

Yes, I’m trying to improve my math skills

Ok, i’m not in my home, i will see later. But thanks for giving a look! (I was dividing by two, but forgot to make the *0.5)

Multiplying by half nowadays is actually generates the same assembly code, since compilers improved a lot, and actually optimizing divisions by power of 2
I just think that this is still good habbit where you don’t much care about precision and care about performance.

I will give you a code snippet to get you started in around 2 hours, since I dont know how did you inplement the weapon trace function - there could be an edge case which i will show you.

Hey mate, sorry I am short on time :frowning:

But I want to point you some edge cases for your trace function:
You must check if there was a blocking hit in your trace - if there was so just return the hit location, else you shall return start + destination and it will be the point you will shot at.

I did it! I analyzed the “static” code and spawned the project from muzzle to BulletVelocity.Rotation (), the only thing I did not use was “BulletVelocity * = ProjectileSpeed”, because I already have a ProjectileSpeed on MovementComponent with an GravityScale value. Thanks a lot you all, i have learned so much with this, and i’m trying to improve my math. Thanks for the patience. (A little of OFF, Dindi#6313 my discord if you guys want to talk sometime! (: ) Cheeeeeers!

Hey, great to hear that you solved this :-).
I preffer chat apps like skype :stuck_out_tongue: you can find me there skype name “roma-mtfdb”
I don’t have an internet connection right now… probably will be back in couple of hours.