|
|
|
|
Reply From: |
DaddyMonster |
You’ll need to code your own custom method for this, look_at
does what it says on the tin and snaps the obj to the target on the negative z.
Several ways to do this. You could take the target’s vector and instead of setting the missile’s vector directly as look_at
does you could lerp towards it. Linear interpolation is a bit on the advanced side though if you’re a beginner. So I’d recommend just comparing the direction to the target against your heading and rotating the obj accordingly checking each frame how aligned they are.
Getting the unit vector to the target is done with target_vector
minus missile_vector
and then normalising the result (bringing the values between zero and one, so just considering the direction, not the scale). This:
var target_vec = (target_obj.global_transform.origin-missile_obj.global_transform.origin).normalized()
Now you can calculate the dot product of your actual heading against your target heading:
var missile_heading = -missile_obj.global_transform.origin.z.normalized()
var dot_prod = missile_heading.dot(target_vec)
If you don’t know, dot product is a calculation where if two unit vectors are aligned then it outputs 1 and if they’re at right angles you get zero. Opposite way outputs -1. This way you can make the missile lose track in a realistic way if it’s outside a “viewing cone”. You’ll need to take orthogonal dot products to establish the bearing (the dot product says they’re not aligned, it doesn’t say which way) and then you can just rotate_object_local
the missile according to these values with a maximum rotation set.
Finally, you just need to set a member variable with your max turn rate and put that in your rotate_object_local
call so that it never turns but more than however many radians you set.
Dot product is about the most useful thing in games so it’s well worth looking at a few tutorials.
Hope that helps.
Why is var missile_heading = -missile_obj.global_transform.origin.z.normalized()
the missile heading? And what is this z
… I’m a noob regarding global_transform still
clemens.tolboom | 2021-02-09 12:39
Ah, the wonderful world of linear algebra.
Let’s do 2d to keep it simple: imagine you have something at Vector2(2, 0) and you add [move it by] Vector2(2, 1). You get Vector2(4, 1). Try to imagine this on graph paper with the missile moving.
Well, in this case you’re just doing this working backwards: your missile is at Vector2(2, 0), the target is at Vector(4, 1). What you want is the vector arrow pointing from the object to the target.
Just minus the target from the object. Vector2(4, 1)-Vector2(2, 0) = Vector2(2, 1). Exactly the same maths as before.
Now, you just want the heading so you normalise it: Vector2(2, 1).normalized(). Dot product won’t work as expected if things aren’t normalised. So, now we know the target is in the positive x direction and a bit on the y direction. (print this to console to see what the actual result is)
Btw, there’s a video course on maths for game devs on humble bundle for 1 EUR atm which covers this. I recommend it.
DaddyMonster | 2021-02-09 13:21