Jump to content


Photo

NPC sprite change based on direction and movement


  • Please log in to reply
9 replies to this topic

#1 Zinth

Zinth

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8

Posted 14 April 2012 - 05:30 AM

Hello every one, I hope I post this right as I'm new here

I am working on a game and it has npc's move towards a target the player sets, and I got that working ok. However I'm having trouble getting the npc's sprite to change when its moving in a certain direction. I have been working on this problem a few days now and finally started learning GML to try and get to the core of the problem.

Here is my obj info

Information about object: ZM1Obj
Sprite: ZM1RgihtStill
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: 
Mask: 

Step Event:

execute code:

// check for movement and directoin and set sprite to match.

if (vspeed > 0 && direction == 90) // see if the sprite is moving up
{
    sprite_index = ZM1Up; // set sprite to moving up animation
}
else if (vspeed == 0 && direction == 90) // check if the sprite is just facing up
{
    sprite_index = ZM1UpStill; //set sprite to standing still facing up
}
else if (vspeed < 0 && direction == 270) // check if sprite is moving down
{
    sprite_index = ZM1Down; //set sprite to moving down animation
}
else if (vspeed == 0 && direction == 270) // check if sprite is facing down
{
    sprite_index = ZM1DownStill; // set sprite to standing still down
}
else if (hspeed > 0 && direction == 0) // check if sprite is moving right
{
    sprite_index = ZM1Right; // set sprite to moveing right animation
}
else if (hspeed == 0 && direction == 0) // check if sprite is facing right
{
    sprite_index = ZM1RgihtStill; // set sprite to facing right sprite
}
else if (hspeed < 0 && direction == 180) //check if sprite is moving left
{
    sprite_index = ZM1Left; //set sprite moving left animation
}
else if (hspeed == 0 && direction == 180) // check if sprite is facing left
{
    sprite_index = ZM1LeftStill; // set sprint facing left sprite
}
else
{
    
    sprite_index = ZM1LeftStill; // change sprite to face left instead of default right to make sure code works.
}

perform a step towards position (TargetObj.x,TargetObj.y) with speed 3 stop at solid only
for other object: perform a step relative towards position (0,0) with speed 0 avoiding solid only
Collision Event with object ZM1Obj:

bounce not precisely against all objects
set the vertical speed to 0
set the horizontal speed to 0


I think the problem is that its moving at diagonals, if anyone can help me fix the code or suggest a better way of doing it please let me know. I would prefer a 4 directional movement system and I'm not sure if that possible using a move to target D&D code.
  • 0

#2 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 14 April 2012 - 06:11 AM

Well which would you prefer, 4 directional pathfinding, or fix your sprites for diagonal? The latter is actually very easy, the former is kinda difficult depending on the game.
  • 0

#3 Zinth

Zinth

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8

Posted 14 April 2012 - 06:25 AM

Well which would you prefer, 4 directional pathfinding, or fix your sprites for diagonal? The latter is actually very easy, the former is kinda difficult depending on the game.

Well the game I'm trying to make is a zombie hybrid were you are a zombie and have the ability to set a location were the more mindless zombies in the area will go to attack, I got the target set to create with a mouse click and they move there (no walls yet just an empty room) but its ugly, I don't mind them moving in diagonals I would just like them to have the right direction sprite when moving and I'm using rpgmaker 2003 sprites so I'm stuck with a 4 directional look (I'm not an artist, and only a beginner programmer just have several game designs in my mind).

So to answer this more clearly I don't mind Diagonals if there's a way to fix the sprites. I like the idea of the zombies running into walls to get to the target and half the game is about getting them to follow your directions. Though I would love to see an example of code on 4 directional path-finding if you know of one for future reference as that might come in handy for the humans I plan on putting in.

And thanks for the quick reply :D
  • 0

#4 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 14 April 2012 - 07:39 AM

Hey again, sorry got wrapped up in some coding :/ Okay so to do the sprites correctly for 360 movement, do something like the below

if (direction => 45 && direction < 135)
{
//face up sprite
}
else if (direction => 135 && direction < 225)
{
//face left sprite
}
else if (direction => 225 && direction < 315)
{
//face down sprite
}
else
{
//face right sprite
}

As for 4 directional movement... well you'd probably have to use mp_grid, and that's pretty advanced.
  • 0

#5 Zinth

Zinth

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8

Posted 14 April 2012 - 02:56 PM

Well that worked awesomely thanks, My zombies now face the right direction when moving however got another problem. I can't seem to get it to detect speed so I can make the walking animation work when moving and it be still when not. Right now I get the still facing the right direction and hovering towards the object.

Things I've tried

if (direction => 45 && direction < 135 && vspeed > 0 )
{
sprite_index ZM1Up;
}

I just thought of something in a room is up +# and down -# or is it the other way around for the y axis.

but then I tried


if (direction => 45 && direction < 135 && speed >0)
{
sprite_index ZM1Up;
}

and still nothing. I tried looking up ways of detecting speed and what I found is current_speed, but when I type that in it doesn't have a built in variable for it.
also I've tried putting it at beginning of step, end of step and nothing.

I'm going to keep trying but any more awesome tips and advice would be much appreciated.
  • 0

#6 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 14 April 2012 - 05:09 PM

This may be a problem with the D&D code, I'm not sure. If "perform a step" in D&D is converted to mp_potential_step(), then I believe your "speed" is actually 0 even when you'd think it is 3. So you'll need to check something other than speed. In any case, you should also have your sprite code at the very end of the step event, definitely after your movement and bouncing code.
  • 0

#7 Zinth

Zinth

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8

Posted 14 April 2012 - 08:56 PM

Ok thanks I will try just doing all the things in GML code so I can see whats happening better, and I think its good practice to do it that way anyways. I just hadn't before because I didn't know if I wanted to go that in-depth into this game but I will and use it as a way to learn GML instead of just using GML as patchwork for the things I can't do in D&D.
  • 0

#8 _258945

_258945

    GMC Member

  • New Member
  • 20 posts
  • Version:GM8

Posted 15 April 2012 - 02:21 AM

I don't know if you figured this out yet, but I was working on the same problem with an 8 directional movement sprite, which is how I found the thread. So this is what I got to work:
/////step event
if variable_triggers_movement = true
{
         if (direction < 270 && direction > 180)
         {
                  sprite_index = spr_to_move_xnegative_ypositive
          }
         else if (direction < 315 && direction > 225)
         {
                 sprite_index = spr_to_move_xneutral_ypositive
          }
         ////if statements continue for each direction
}
image_speed = 1

Hope it helps.
  • 0

#9 Zinth

Zinth

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8

Posted 15 April 2012 - 03:14 AM

I found some code in one of the tutorials and have mixed it with some of my own to get a semi decent moving to target AI going and I fixed the movement problem

go to AI
var follow_obj;

follow_obj = TargetObj

if (instance_exists(follow_obj))

{

 mp_potential_settings(90,90,3,false);

 mp_potential_step_object(follow_obj.x, follow_obj.y, 3, ZM1Obj);

 direction = round(direction/90)*90;
 if (distance_to_object(follow_obj) <= 2)

 {

   x = xprevious;

   y = yprevious;

 }
 

}

Sprite Change based on movements and direction
if (speed > 0 )
{
    if (direction >= 45 && direction < 135)
    {
        sprite_index = ZM1Up;
    }
    else if (direction >= 135 && direction < 225)
    {
        sprite_index = ZM1Left;
    }
    else if (direction >= 225 && direction < 315)
    {
        sprite_index = ZM1Down;
    }
    else
    {
    sprite_index = ZM1Right;
    }
}
else
{
    if (direction >= 45 && direction < 135)
    {
        sprite_index = ZM1UpStill;
    }
    else if (direction >= 135 && direction < 225)
    {
        sprite_index = ZM1LeftStill;
    }
    else if (direction >= 225 && direction < 315)
    {
        sprite_index = ZM1DownStill;
    }
    else
    {
    sprite_index = ZM1RgihtStill;
    }
}



I got some tweaks to make yet as I also want these things to hunt other-things unless given an order.

I hope this helps with anyone who had similar problems that I had, and thanks for all the help greep your a life saver.

Edit: Never mind about it fixing the movement animation I just switched my sprites to rpgmaker xp style using My link to make sprites and it doesn't work, got to find a new way to do it.

Edited by Zinth, 15 April 2012 - 04:47 AM.

  • 0

#10 Zinth

Zinth

    GMC Member

  • New Member
  • 6 posts
  • Version:GM8

Posted 15 April 2012 - 05:07 AM

Got my code working now with full animation change and everything just had to make my own variable and set it several times in the code depending on the situation. Called the variable current_speed. Also discovered I can combine all my code into one script which made it easier to manage.

// varaibles
var follow_obj;
var kill_obj;
var current_speed;

follow_obj = TargetObj
kill_obj = HumanParent_obj
current_speed = 0

// set up move to target code
if (instance_exists(follow_obj))

{

 mp_potential_settings(90,90,3,false);

 mp_potential_step_object(follow_obj.x, follow_obj.y, 3, ZM1Obj);

 direction = round(direction/90)*90;
 
 current_speed = 3
 
 if (distance_to_object(follow_obj) <= 2)

 {
    current_speed = 0
    
   x = xprevious;

   y = yprevious;

 }
 

}

// set up move to human code
if (instance_exists(kill_obj))
{
    if (distance_to_object(kill_obj) <= 20)
    {
        if !(collision_line(x,y,kill_obj.x,kill_obj.y,Wall_obj,false,true))
        {
        
            mp_potential_settings(90,90,3,false);

            mp_potential_step_object(follow_obj.x, follow_obj.y, 3, ZM1Obj);

            direction = round(direction/90)*90;
            
            current_speed = 3
        }
    }
}

// set up movement system
if (current_speed > 0 )
{
    if (direction >= 45 && direction < 135)
    {
        sprite_index = ZM1Up;
    }
    else if (direction >= 135 && direction < 225)
    {
        sprite_index = ZM1Left;
    }
    else if (direction >= 225 && direction < 315)
    {
        sprite_index = ZM1Down;
    }
    else
    {
    sprite_index = ZM1Right;
    }
}
else
{
    if (direction >= 45 && direction < 135)
    {
        sprite_index = ZM1UpStill;
    }
    else if (direction >= 135 && direction < 225)
    {
        sprite_index = ZM1LeftStill;
    }
    else if (direction >= 225 && direction < 315)
    {
        sprite_index = ZM1DownStill;
    }
    else
    {
    sprite_index = ZM1RightStill;
    }
}

now to figure out collision coding and get my human hunter code working and my zombies will be done :D
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users