Top-Down 2D Sprites, basic programming, CharacterMotor.js

Hi all,

I’m new. I’ve gone through the Unity tutorials and have just started to play with the program. But I am still learning the programming side of things. I am working on a top-down game (think Pokemon) where the character walks around and cannot jump. I’ve opened up the CharacterMotor.js script in hopes of deciphering it.

I can make animations later, but for now I’m trying to script:

  • When the First Person Controller moves, find out what direction / rotation and change the image / sprite accordingly.
  • If the character has stopped moving (no further input) then find out direction and change the image / sprite accordingly.

Any and all help would be greatly appreciated, although I will note that without specific instruction, I won’t know where to add specific lines of code.

Thanks!

If it helps I was thinking of something like this:

if(velocity > 0)

{
//velocity > 0 means the character is moving, so these are run animations
if(337.5 degrees < rotation < 22.5 degrees) {change material = up-runanimation}
if(22.5 degrees < rotation < 67.5 degrees) {change material = upright-runanimation}
if(67.5 degrees < rotation < 112.5 degrees) {change material = right-runanimation}
if(112.5 degrees < rotation < 157.5 degrees) {change material = downright-runanimation}
if(157.5 degrees < rotation < 202.5 degrees) {change material = down-runanimation}
if(202.5 degrees < rotation < 247.5 degrees) {change material = downleft-runanimation}
if(247.5 degrees < rotation < 292.5 degrees) {change material = left-runanimation}
if(292.5 degrees < rotation < 337.5 degrees) {change material = upleft-runanimation}
}

else

{
//velocity is 0, so these are standing animations
if(337.5 degrees < rotation < 22.5 degrees) {change material = up-standanimation}
if(22.5 degrees < rotation < 67.5 degrees) {change material = upright-standanimation}
if(67.5 degrees < rotation < 112.5 degrees) {change material = right-standanimation}
if(112.5 degrees < rotation < 157.5 degrees) {change material = downright-standanimation}
if(157.5 degrees < rotation < 202.5 degrees) {change material = down-standanimation}
if(202.5 degrees < rotation < 247.5 degrees) {change material = downleft-standanimation}
if(247.5 degrees < rotation < 292.5 degrees) {change material = left-standanimation}
if(292.5 degrees < rotation < 337.5 degrees) {change material = upleft-standanimation}
}

Yes, yes… I realise I don’t know the language yet. These IF statements just check the rotation and speed, and then change the image of the characters player (which is on a plane because it’s 2D). But as for the specifics of what all the variables and things are called… I don’t know. I’m currently looking through CharacterMotor.js trying to understand it.

Don’t worry about fixing it at the moment until you need to refactor the code later. Just for the basis of your game. I think it would be better to check for input having both arrowdown and arrow right, arrow right etc. That saves on having to be so precise on degrees which doesn’t obey the principle of avoiding magic numbers. Either way its fine.