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

**URL:** <https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348>\
**Category:** UI\
**Tags:** crosshair, thirdperson, tps, unreal-engine, question, CPP\
**Created:** [October 18, 2017, 8:45pm UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348 "2017-10-18T20:45:28Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 20, 2017, 1:53am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/21 "2017-10-20T01:53:24Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [October 20, 2017, 1:56am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/22 "2017-10-20T01:56:40Z")

</div>

It’s all for the digital karma! 😛

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 20, 2017, 2:02am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/23 "2017-10-20T02:02:21Z")

</div>

Which is quite useless, you can’t even make a bounty here (Or can I ? )

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [October 20, 2017, 2:12am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/24 "2017-10-20T02:12:22Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 20, 2017, 2:19am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/25 "2017-10-20T02:19:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 20, 2017, 2:21am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/26 "2017-10-20T02:21:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Varmillion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Varmillion](https://forums.unrealengine.com/u/Varmillion)\
**Post date:** [October 21, 2017, 5:29am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/27 "2017-10-21T05:29:04Z")

</div>

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](https://imgur.com/a/jnKqT)

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()

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 21, 2017, 8:50am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/28 "2017-10-21T08:50:39Z")

</div>

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 🙂

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());

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_f77a82de](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f77a82de](https://forums.unrealengine.com/u/anonymous_user_f77a82de)\
**Post date:** [October 21, 2017, 3:08pm UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/29 "2017-10-21T15:08:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Varmillion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Varmillion](https://forums.unrealengine.com/u/Varmillion)\
**Post date:** [October 21, 2017, 9:37pm UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/30 "2017-10-21T21:37:19Z")

</div>

Yes, I’m trying to improve my math skills

---

<div class="post-metadata">

**Author:** ![Varmillion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Varmillion](https://forums.unrealengine.com/u/Varmillion)\
**Post date:** [October 21, 2017, 9:41pm UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/31 "2017-10-21T21:41:05Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 21, 2017, 9:56pm UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/32 "2017-10-21T21:56:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 22, 2017, 1:27am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/33 "2017-10-22T01:27:02Z")

</div>

Hey mate, sorry I am short on time ☹

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.

---

<div class="post-metadata">

**Author:** ![Varmillion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Varmillion](https://forums.unrealengine.com/u/Varmillion)\
**Post date:** [October 24, 2017, 1:11am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/34 "2017-10-24T01:11:04Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 24, 2017, 1:55am UTC](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348/35 "2017-10-24T01:55:31Z")

</div>

Hey, great to hear that you solved this :-).  
I preffer chat apps like skype 😛 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.

[Previous page](https://forums.unrealengine.com/t/how-can-i-shoot-at-the-center-of-my-crosshair-c/407348.md?page=1)
