08-24-2014, 09:18 AM
(08-14-2014, 11:26 PM)Epichao Wrote: On my end, Unity makes me cry, but I'm working on a little wasp game with it. I actually got the basic controls working, but now I need to make the wasp move instead of teleport to where I click. Probably gonna play with that code some more before I call it a night.
Unity is a pain - it's got a lot of awkward quirks (some of them frankly stupid), and why they have Javascript as a language I'll never know. I firmly recommend using C# if you're going with Unity. If you're new to coding, it looks daunting, but actually it's more friendly to newcomers, because Javascript hides a lot of things (like types) that can cause serious problems if they're done wrong.
As for solving this problem, make a Vector2 variable (assuming you're in 2D space, if not, use Vector3) to record where you clicked, keep another one which controls the speed of the wasp, and then take the difference between your target vector and your current position as a direction vector (normalised and multiplied by your acceleration), and add that to your speed each frame. This will accelerate your wasp towards where you click.
The difficulty is in stopping it once you get there. I recommend making it start decelerating once you hit a certain speed/are within a certain distance. To work out exactly how to decelerate it, you'd use the SUVAT equations. Specifically, v^2 = u^2 + 2as. Here, v would be 0 (not moving), u is your current speed, s is the distance between you and the target point, and a is the value you want, so you'd be calculating: a = -u^2/2s, which gives you your acceleration in order to hit 0 speed at the target point.
If you're looking at something like Unity or anything more powerful for game development, be prepared to get into some maths.