I’m making a tower defense game, and need to take into account the enemy’s movement speed, movement direction, and the tower’s projectile speed. I already have a way of getting the target enemy’s movement speed, but how do I do the rest? Thanks!
Depends on what you define as “ahead”.
The ‘Transform’ property has various members for right/up/forward axes which map to the x/y/z axes (negate for opposite). Whatever one you consider to be “forward” is your “forward”. (by default the named one forward is the z axis, but since you’re 2D… pick one that maps appropriately for you)
“Forward” is the direction of movement, which changes over time
I mean what you consider forward relative to the Transform.
You define forward for yourself… you should consistently use the same axis of the Transform for every ‘forward’ in the game.
In 3D we generally use the z-axis for ‘forward’, and it’s why it’s named ‘forward’.
In 2D, honestly I don’t know what people generally use in Unity (I don’t do 2D games in Unity much), but in other engines I usually used the x-axis, which is the ‘right’ property of the Transform.
In the end though it’s up to you to choose one. Once chosen you just consistently use it for all your movable entities (when you create prefabs you face them visually so that they look correct moving in said ‘forward’ direction). That’s what I mean by ‘define’… not define to me… define in general. What direction do you want to be considered “forward”?
…
For example when you move, how do you move? What axis do you move along and position your sprites? That’s… your forward!
That really depends on how you’re moving the game object. If it’s a rigidbody, then probably just its velocity. Otherwise, you probably ensure it’s rotated in the direction its headed, and use the appropriate transform.forward (or whatever one makes sense given the perspective) and aim ahead given it’s speed.
Ah did you mean like intercepting or, khm, leading shots? Why didn’t you say so.
Well you could check out this humongous tutorial, where the dude almost died (because of how long it is obviously). It’s for 2D, and boils down to a surprisingly small footprint (unlike the tutorial itself). Anyway there you can find the entire in-editor demo (and visualization) and the text will explain everything, at least I hope it does.
- Edit: Note that the tutorial is written for the older forum software, and it’s somewhat unreadable now due to spoiler buttons being converted to blur boxes.