Hi.
I apologize for the hackneyed question, but I couldn’t find the answer on the forum.
I’m making a tank.
Simple hierarchy: Body → tower → cannon.
A little stuck on the aiming system.
I’m doing a linetrace from the screen to the world. A hit position appears in the world. I need the tower to rotate to this point around its local Z axis, and the gun along Y;
A static turret is very easy to implement, since I can use world coordinates, but here I need to rotate this components relative to the body of the tank, since when moving on inclined surfaces, the rotation is incorrect if I use world coordinates.
If possible, an example in C++ would be very helpful, since I’m not very good at blueprints…
I’m assuming you would use the same setup in CPP as BP, which would be ‘look at rotation’. But for this to work, your gun needs to point along the local X axis.
Yes, I see that “FindLookAtRotation” simply subtracts the “start” and “target” vectors to find the direction.
What should I do with this direction then?)
In C++ it looks something like this, but unfortunately it doesn’t work. The turret rotates to the desired point only if the tank itself is pointed towards the world X axis. When the tank rotates 90 degrees, the turret also looks to the target direction in the direction, but with + or - 90 degrees and so on…(
Yes, as I already said, what you show is very easy to do, since these are two actors, and they are both located in world coordinates. The only thing is that I need to rotate not a separate actor, but a component that is lower in the hierarchy of the main actor itself… The hierarchy is like this:
Tank
|
Tower - Rotate along local Z towards the target
|
Cannon - Rotate along local Y towards the target
The tank also rotates when it moves…)
This method does not work in the local coordinates of the component…(
But an actor is not a component…
You can clearly see SetActorRotation() in your screenshot)
Or am I missing something?)
Will the result be the same even if you rotate around various axes the tank itself?)
By the way. Please tell me how you make such small videos? What soft you are using for this?) It would probably be very cool and simple if I could do the same to show what exactly I’m doing)
You’re right, my CPP example only contains actors, sorry. I’ll see if I can find something more relevant… But it will be ‘set relative rotation’ on the component.
Sorry, I can’t load the video… The Internet connection is interrupted all the time.
Alas, “SetRelativeRotation” preserves rotation in other axes but sets the Z rotation in the target direction incorrectly, and “SetWorldRotation” rotates in the target direction correctly, but does not prevent rotation in other axes…(
Well, this will take some time)
I’ll be sure to answer as soon as I get something done)
Thank you in advance. From local to world and back looks very logical in this situation. I hope it will work
Perfectly works)
Just awesome! This is the very first time that someone was really able to help me)
Eternally grateful to you)
The code looks like this) I hope this will help someone else too)