GaryS Posted February 15, 2016 Share Posted February 15, 2016 Please excuse what I'm sure is a very novice question, but I've been all over Google and I can't find what I'm looking for. I'm attempting to detect when a sprite is no longer being moved by the player - e.g. when the player releases the key. I'd like to do this by seeing how the sprite is moving, rather than relying on detecting the keypress or other input, as the sprite has some drag associated with it and I'd like to play a 'slowdown' animation as the drag slows the sprite. I'm also attempting to keep as much encapsulated in my prefab as possible and thus I don't want to put anything in the main game's update method to initiate the slowdown function. So, what I'd like to do is detect that the sprite is no longer moving at the veliocity I'd expect it to if the move functions were being called... The thought being that the velocity is constant while the key is down, and begins to reduce when the key is released - however because the sprite can be at any angle, I can't predict what the velocity will be without understanding how it's calcuated. So, unless someone has another suggestion as to how I might detect that the sprite is slowing down, in any direction - would someone be able to give me some schooling in what I assume is very basic trigonometry? Thanks! Link to comment Share on other sites More sharing options...
Tom Atom Posted February 15, 2016 Share Posted February 15, 2016 First set your "limit" speed. When object velocity falls below you are sure object is slowing. Object's final velocity is composed from its x velocity and y velocity. Ratio and sign of these two gives direction and size in 2D space - velocity vector (velX, velY). To calculate vector size you have to do square root of velX * velX + velY * velY: lenght =sqrt(velX * velX + velY * velY) ... in fact, it is Pythagorean theorem. If length is below your limit speed, then object slowed down below it. (length < limit) As calculating sqrt for every check is wasting of CPU you can power your limit speed by 2 and compare if: length_squared < limit_powerded_by_2 ... velX * velX + velY * velY < limit_powered_by_2 GaryS 1 Link to comment Share on other sites More sharing options...
GaryS Posted February 15, 2016 Author Share Posted February 15, 2016 That's exactly what I was looking for, thanks so much! Link to comment Share on other sites More sharing options...
Recommended Posts