gravity enemy units

Posted By: Denn15

gravity enemy units - 09/25/14 20:13

i want to make enemy units in my game be affected by gravity, so that when they walk off a ledge they fall down. ive tried to use c_trace to check whether or not the ground is under the units which will increase a local variable that makes the units fall down to the ground faster and faster until they hit the ground where the value is set to 0.

the problem is that half the time they dont react at all after walking off a ledge and other times they slam into the ground and other times they fall down as they should but it is not consistent at all.

Code:
action enemy()
{
	var grav;
	while(my.health > 0)
	{
		if (c_trace(my.x, vector(my.x, my.y, my.z - 15), IGNORE_ME) == 0)
		{
                        grav = grav + 1 * time_step;
			c_move(me, nullvector, vector(0, 0, - grav * time_step), GLIDE);
		}
                if (c_trace(my.x, vector(my.x, my.y, my.z - 15), IGNORE_ME) != 0)
                {
                        grav = 0;
                }
		wait(1);
	}
	ent_remove(me);
}



have i done something completely wrong or is there a better method of doing it?
Posted By: Superku

Re: gravity enemy units - 09/25/14 21:22

Have you had a look at the following article plus example code/ zip file: http://opserver.de/qwik8/index.php?title=Gravity ?
Posted By: Wjbender

Re: gravity enemy units - 09/26/14 12:18

by the way ,you are also multiplying by time_step twice there in your gravity code and movement code
Posted By: Superku

Re: gravity enemy units - 09/26/14 12:19

Where?
Posted By: Denn15

Re: gravity enemy units - 09/26/14 14:12

ive tried using the code you sent me for the enemy units and it works fine except for sometimes where some units fall faster than others.

I just dont understand why the code i made before wouldnt work. my code works on the player character but when if i use the same method for more than one unit of the same type it just doesnt work.
Posted By: Superku

Re: gravity enemy units - 09/26/14 20:17

You need to initialize local variables, grav could have any random value, maybe that is the cause for your problems.
Btw. I would not use the c_trace return value for ground detection but trace_hit or the hit struct (or the HIT_TARGET macro or whatever it is called). Calling c_trace two times in a row with the same parameters is pretty wasteful, too. Just use an else case instead.
Posted By: Denn15

Re: gravity enemy units - 09/26/14 20:39

yea you are right. but grav should be a local variable which is why i dont understand it
Posted By: Superku

Re: gravity enemy units - 09/26/14 20:56

I think you misunderstood me. The content of grav as a local variable is preserved (/ restored) during and after each frame but its initial value could be any random value. Give the following example a try:

Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

action enemy()
{
	var grav;
	printf("%.3f",(double)grav);
	wait(1);
	sys_exit("");
}

void main()
{
	level_load(NULL);
	ent_create(CUBE_MDL,nullvector,enemy);
}



The solution to this obviously is to write "var grav = 0;" instead.
Posted By: Denn15

Re: gravity enemy units - 09/26/14 21:00

oh i see. grav seems to be 1040384.000
© 2024 lite-C Forums