# Rotate mesh towards Toss velocity

Hello, I am having a bit of a problem understanding how to rotate mesh towards toss Velocity. I have a mesh with a socket that points to a target.

I have used SuggestProjectileVelocity to find out the toss velocity which is coming out to be fine as in the image.

Now the problem is at what angle should I rotate as the toss velocity is coming from the socket location but I want to turn my mesh whose pivot point would be different from the socket location. Like what would be theta1

I hope I am able to explain my problem. Thank you

Hey @FinalRockstar, so if Iâ€™m understanding the problem correctly, youâ€™re pathing the arc from the projectile socket but want to rotate the gun itself and not a projectile of it, and you just want the gun itself to rotate towards itâ€™s current velocity?

I myself have only ever used the projectile component since it has this functionality built in, however thereâ€™s more custom approaches to this. I came across a thread that does just a basic turn object to itâ€™s velocity but I feel like Iâ€™m missing something with the casting from the projectile shooting socket.

Let me know if Iâ€™m off base, a clarification might get me sorted!

2 Likes

Hi Yes you understood the problem correctly . I want to rotate my mesh towards the toss velocity. But I have a question . I have calculated Toss velocity from socket so if I normalize and find the direction from socket then the direction which is pointing at, will it be same for mesh also ? So I just need to use find look at Rotation function where start would be mesh location and target would be the normalized toss velocity? Or do I need to apply some more calculations? I will also try with projectile component if I am unable to solve. Thanks for the info

1 Like

Hi there,
If you want both shots to match the target location, you may want to try this node:

Start Pos will be different for each actor, but End Pos will be the same.
Then, you can use find look at rotation to rotate your mesh towards the target position with Set Actor Rotation

1 Like

If you are working from that socket specifically I think youâ€™d have to do an arc from the gunâ€™s pivot/center of mass and use that instead as unless youâ€™re prepping the arc for the projectiles themselves instead. You could do this from a different socket, the sm itself, or youâ€™d have to overlay some more math over the arc from the projectile socket, but thatâ€™d be more complex than just doing the arc with the gun itself

1 Like

Hi thank you for reply so I want to ask what is the difference between both these functions other than custom arc has fixed arc of 45 degree. Also I have only one actor to shoot which is having a socket (not multiple actors) . I am using suggest velocity from socket and if I use the result from that and rotate actor from center of mass then wonâ€™t it be wrong ?

If I use suggest velocity from center of mass of mesh then wonâ€™t the projectile overshoot the target? And if the center of mass is way back then the result could be very far ?

You can use the socket location instead. The start pos is the actual spawning point. Then use the suggested velocity to set your projectile initial velocity right after spawn actor (projectile) from class.

Arc Param = 0.5 is about 45Âş degrees

I guess you should calculate the find look at rotation for aligning the canon angle, then calculate spawn velocity with the suggested node. From what I could see, the canon is only a component of the tank, correct? In this case, you can use control rig or â€śskeleton controlâ€ť transform modify bone to rotate in world space.

1 Like

Yes so I will use the socket location to find toss velocity and will set the initial velocity of projectile to the suggested velocity that part is clear, I am more concerned with the alignment of cannon at a correct angle.
â€śI guess you should calculate the find look at Rotation for aligning the cannon angleâ€ť - so the target would be the toss velocity from suggest velocity function which used socketâ€™s location as start position? Will that be correct ? I will need to check it.
Yes itâ€™s a part of component of a tank but it doesnâ€™t have any skeleton.

Yes, you can you the node â€śfind look at rotationâ€ť to set the cannon rotation in world space. If the canon is a child actor of the tank, it will be easier to get its actor rotation. The start pos for the cannon should be the canonâ€™s pivot point. The end pos is the target. Also, the start pos for the shot is the socket location, and the Pitch Y Axis angle should be 45Âş. So, the only angle missing is the Yaw ZAxis taken from find look at rotation.

First you align the cannon to the target, then calculate the shot velocity and spawn the projectile from the socket location. The projectile velocity is set by the suggested velocity after spawn actor from class.

I hope that makes sense. Iâ€™ll check up on you tomorrow.

1 Like

Thank you for taking the time out. I will come up with the update as soon as possible

Great.
Looking forward to seeing if works.

Hello, So I have tried to use Socket as well as Mesh as the start Position. Here is my observation

#### Blueprint Setup

I have changed My mesh to be a very long box. I did this to figure out any issues easily as with the Gun it was shooting target correctly because the difference between socket position and gun center of mass position was not enough.

And Shooter Actor is a simple one that just rotates

As you can see in both the setup the shooter actor is able to turn correctly but the problem is for Pitch as in one it overshoots and in the other it undershoots.

Hi there,
Thank you for sharing the logic, that helps to understand what is causing the issue.
Youâ€™re almost there. I believe youâ€™ve missed that â€śfind look at rotationâ€ť:

If youâ€™re changing the cannonâ€™s pitch, this information should be feeding into the projectile calculation, however only the node â€śSuggest Projectile Velocity with Custom Arcâ€ť has such parameter exposed in Blueprint. Keep in mind that the cannonâ€™s pitch of 0 - 90Âş equals to 1 - 0 in that node (â€śArc Paramâ€ť). So, youâ€™ll need a â€śmap range clampedâ€ť node.

In my timelapse video, I show how to use that node with a projectile launch at time 1:03

Hope that helps.

1 Like

Thank you for taking out time to answer. If I use ActorToPointâ€™s Location in FindLookAtRotation then the Shooter Actor or Cannon will directly point to Target instead of slight inclination. Although by setting velocity the projectile hits the right target. This inclination problem may or may not be noticeable. As of now, I am happy with the solution you provided : )

As for â€śSuggest Projectile Velocity with Custom Arcâ€ť what is the difference between Out Launch Velocity and Toss Velocity of â€śSuggest Projectile Velocity"? I am sorry for asking too many questions.

1 Like

Hi there,
Glad it helped you. The velocities are the same, the different is that you have more inputs to control your trajectory with Suggest Project Velocity with Custom Arc. Also, I believe Out Launch Velocity is a direct reference to the input used in the node Lauch Character.

Keep up the good work.

1 Like

Thank you

Hi apologies for coming again on this topic but I am facing some issue with Suggest Project Velocity with Custom Arc for the same project.The issue is
in Infinity Elven Ruins Level the Gun is Aiming wrongly. i.e it is aiming downwards. But in all other levels the gun is aiming correctly i.e in an arc. I donâ€™t know whether it is level specific issue or issue with the code. Here in the Function AimDirection is the target position. I am trying to rotate my Gun in a direction so that it can hit a target while aiming in an arc. Is it a right way of doing?

``````void UMortarProAimingComponent::CalculateAimDirection(FVector& AimDirection)
{
FVector OutLaunchVelocity = FVector::ZeroVector;
UGameplayStatics::SuggestProjectileVelocity_CustomArc(GetWorld(), OutLaunchVelocity,
Gun->GetCenterOfMass(),
AimDirection);

AimDirection.Z = OutLaunchVelocity.Z;
}
``````

Here is the expected gun direction. In other levels it is working fine.

And Here is the actual gun direction in Infinity Elven ruins.

And After calculating the AimDirection I adjust the Gun Elevation.
`Barrel->Elevate(AimDirection);`

``````void UMortarProBarrelMeshComponent::Elevate(const FVector &ElevateDirection)
{

FRotator BarrelDesiredRotation = UKismetMathLibrary::FindLookAtRotation(GetComponentLocation(), ElevateDirection);

FRotator BarrelDeltaRotation = FMath::RInterpConstantTo(GetComponentRotation(), BarrelDesiredRotation, GetWorld()->DeltaTimeSeconds, 180);

FRotator BarrelOriginalRotation = GetComponentRotation();

//Only Require To Update Barrel Pitch
float ProcessedElevation = FMath::Clamp<float>(BarrelDeltaRotation.Pitch, -30, 70);
BarrelOriginalRotation.Pitch = ProcessedElevation;

SetWorldRotation(BarrelOriginalRotation);
}
``````

Hi there,

Can you please record a short video showing the issue in the Elven Ruins? Are any of the actors below zero altitude? It might be a calculation error that is causing the cannon to look downwards, for instance, if the end position is below the tank, and youâ€™re using find look at rotation, perhaps you want to constraint the values of pitch so the cannon will never look down. Please record some videos or screenshots and I get back to you tomorrow morning.

The red sphere denotes the position of the target, and the green sphere is the direction in which the gun should aim to hit the target. I discovered this is not specific to Elven Ruins, as in the map, the ground level is at around 7000 Z units. If I keep the gun near the origin, then it works fine, but if I keep the gun way above the ground, then it points downward. I feel there is some calculation mistake going on.