Welcome Guest [Log In] [Register]
Welcome to the Game Maker Cookbook. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Strafing in Top-Down View
Topic Started: November 14, 2008, 7:34 pm (3,307 Views)
Poris
Member Avatar
Administrator
How to make an object strafe around the mouse in Top-Down View
Made by Daniel Seabra
Requires PRO version of Game Maker.

Definition of Strafing:
Wikipedia
 
This term has been adopted by certain gamers to mean "sidestepping", primarily in first-person shooters (FPS); in this context, it refers to the movement alone, even when no weapon is being fired. Sidestepping is an integral part of any first-person shooter as it allows the player to dodge incoming fire while keeping their view aimed at their target.



Problem
You want the player strafe when you press down the left (and right) key(s) (assuming the player is looking at your mouse) and your game is top-down.

Solution
The following code should take place in the step event:

Code:
 
if keyboard_check(vk_left) //Left Strafe
{
x-=lengthdir_x(speed,direction-90)
y-=lengthdir_y(speed,direction-90)
}

if keyboard_check(vk_right) //Right Strafe
{
x-=lengthdir_x(speed,direction+90)
y-=lengthdir_y(speed,direction+90)
}


Discussion
This code takes advantage of the use of lenghtdir_x and lenghtdir_y, which are lamentably only be able to be used on the PRO version of Game Maker. These functions allow us to calculate where to strafe and how much distance. The variable speed should be replaced with your number for speed.



[/code]
Edited by Poris, November 14, 2008, 7:46 pm.
Offline Profile Quote Post Goto Top
 
AndrewYY

the lengthdir functions convert polar coordinates into x,y coordinates. With a tiny bit of math, they can be emulated:

Code:
 
//ld_x(dist,dir);
return argument1*cos(degtorad(argument0));

Code:
 
//ld_y(dist,dir);
return argument1*sin(degtorad(argument0));
Edited by AndrewYY, July 31, 2009, 5:00 pm.
Offline Profile Quote Post Goto Top
 
Snuffypot11

Ok so I'm new to GML as I've used D&D, but I have been working on my own top down game and this is the code I'm using in a step event on my character.

Code:
 

if keyboard_check(ord("W"))
{
move_towards_point(mouse_x,mouse_y,4)
}
else if keyboard_check(ord("S"))
{
move_towards_point(mouse_x,mouse_y,-4)
}
else if keyboard_check(ord("A"))
{
motion_set(image_angle+90,4)
}
else if keyboard_check(ord("D"))
{
motion_set(image_angle-90,4)
}
else
{
motion_set(0,0)
}


In addition to this of course:

Code:
 

image_angle=point_direction(self.x,self.y,mouse_x,mouse_y)
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Intermediate · Next Topic »
Add Reply