help with enemy movement on a platformer???
CodeMonster
ACT, AustraliaMember Posts: 1,078
ok so i need my enemies to keep moving left and right,
eg;
one of my enemies will start in the middle of a platform and start to move left, when it reaches the end of the platform, it goes right, and then left and so on, so the enemy wont fall off the platform.
any help would be great thanks!
eg;
one of my enemies will start in the middle of a platform and start to move left, when it reaches the end of the platform, it goes right, and then left and so on, so the enemy wont fall off the platform.
any help would be great thanks!
Comments
Constrain X Position to:
AAA*sin(game.time*BBB)+CCC
. . . . . . . . . . . . .
AAA = range of the left to right movement, so if you want the enemy to 'patrol' 100 pixels to the left of his starting position and then 100 pixels to the right his starting position . . . you would make this value 100.
BBB = how fast you want the enemy to move.
CCC = the enemy's starting position.
. . . . . . . . . . . . .
So an actor moving from left to right on a horizontal iPad screen (1024 pixels wide) would be:
512*sin(game.time*100)+512
(the '100' here is an arbitrary speed)
Very useful. Will use it on my next game.
Thank you very much.
@vuifah
>-
GSINVENTION FREE TEMPLATES
when in preview all of the enemies were above eachother and moving from left to right, any reason why this is happening?
GSINVENTION FREE TEMPLATES
Make use of [CCC = the enemy's starting position] to give each actor a unique X position.
GSINVENTION FREE TEMPLATES
Example attached:
GSINVENTION FREE TEMPLATES
If you want multiple actors all following the same constrain behaviour, but with their own unique X position then stick this into the master enemy actor . . . .
Make an attribute (let's call it 'MyUniqueXPosition') use this to store the actor's current X position, then in your constrain behaviour make the CCC part = MyUniqueXPosition.
So:
Change attribute MyUniqueXPosition to Self.Positon X
AAA*sin(game.time*BBB)+MyUniqueXPosition.
. . . . That means the actor will patrol left to right (repeatedly) with a range of AAA, at a speed of BBB and with its patrol range centred on whatever the actor's original X position was.
GSINVENTION FREE TEMPLATES
One final point, if you use the above method (the 'MyUniqueXPosition' attribute) then obviously the method for detecting that the actor has reached the edges of his patrol areas (so you can switch animations from left > right < left > right) mentioned above, which is a simple static X value won't work as the actors will all be at different locations on the screen - so instead use the MyUniqueXPosition attribute to check whether the actor has reached either end of its range.
Something like this: (let's assume the patrol range is 100 pixels each way, left and right).
If X position = (MyUniqueXPosition-100) then use the right facing animation.
If X position = (MyUniqueXPosition+100) then use the left facing animation.
Hope that makes sense.
GSINVENTION FREE TEMPLATES
GSINVENTION FREE TEMPLATES
created a self integer attribute called constrainingXposition
i then constrained self.position.x to 60*sin(game.time*15)+constrainingXposition
next i changed attribute self.constrainingXposition to self.position.X
rule for animation(which isnt working ::
rule if attribute self.position.x = (self.constrainingXposition-60)
then animated right
rule animation left;
rule if attribute self.position.x = (self.constrainingXposition+60)
then animated left
please help me out im confused @Socks
GSINVENTION FREE TEMPLATES
Here is what your process above does . . .
1) You create a variable, it's called constrainingXposition with a default value of zero.
2) You then tell an actor to constrain itself to 60 x the sine of an ever increasing angle + the value of the constrainingXposition variable (which is zero) . . so off it goes, doing it's thing.
3) You then change the constrainingXposition variable to the actor's current position.
. . . . if you can't see what's wrong here I'll tell you if you want me to, but it's always good to see these things for yourself ! Let me know if you want me to tell you what's wrong. >-
but my guess is that its got something to do with the attribute??
GSINVENTION FREE TEMPLATES
1) You create a variable with a default value of zero.
2) You then tell an actor to constrain itself to move left and right about this variable.
3) You then change the variable to the actor's current position.
Swap 2 and 3.
GSINVENTION FREE TEMPLATES
GSINVENTION FREE TEMPLATES
. . . . . . . . . . . . .
Lets imagine the actor's starting X is 317 (or whatever).
1) You create a variable V with a default value of zero.
V =0
2) You then tell the actor to constrain its X position to this variable.
V=0 . . . . so . . . X=0
3) You then change the variable to the actor's X position.
X=0 . . . so . . . V=0
Everything will hang around 0 regardless of it's original X position.
. . . . . . . . . . . . .
Swap 2 and 3
. . . . . . . . . . . . .
Lets imagine the actor's starting X is 317 (or whatever).
1) You create a variable V with a default value of zero.
V =0
2) You then change the variable to the actor's X position.
X=317 . . . so . . . V=317
3) You then tell the actor to constrain its X position to this variable.
V=317 . . . . so . . . X=317
Everything will hang around the actor's original X position.
. . . . . . . . . . . . .
so an example; i place an actor at position 372x then i place all the rules in its prototype, the enemy wont start at this point, nor return to it, it will start like at 300x or maybe 400, im not sure why?
GSINVENTION FREE TEMPLATES
Works fine for me, I stuck a red marker underneath some randomly place blocks to see that they were moving as they should be, works fine.
Example attached:
P.S . . . on a side note . . . . in the main actor switch off the bottom constrain behaviour - and switch on the two rules in the middle that are currently switched off (a change attribute action and a constrain behaviour) - I don't know how your game works, but having a random start position for each enemy looks better to me.
GSINVENTION FREE TEMPLATES
GSINVENTION FREE TEMPLATES
GSINVENTION FREE TEMPLATES
Can you simply take a screenshot of the relevant rules.
GSINVENTION FREE TEMPLATES