#My enemy won't walk randomly until it'll be touched and reaches bottom end.
1 messages · Page 1 of 1 (latest)
So from what I can see, your skeleton is being pushed by the player which is why it slides down
Your motion variable doesn't look like it is used though
Perhaps you are wanting to assign that motion to the velocity variable?
Having said that, it will most likely jitter around since it is potnetially changing its direction every _physics_process frame
I replaced motion to the velocity, but it doesn't changes
this skeleton is still being pushed by the player
Alright start breaking it down. First comment out everything under move_and_slide()
Now on the first like of _physics_process put in velocity.x = 100
This should be above the updateAnimation() and move_and_slide() lines
Remove the state handing
I am trying to see if it moves at all
Just put velocity.x = 100
I expect if it works, it will move to the right
hmm... which should I replace state == 0, state == 1, state == 2, state == 3, and state == 4?
func _physics_process(_delta):
velocity.x = 100
updateAnimation()
move_and_slide()
For now just do this
Just trying to debug the issue for now
Alright nice
Now try putting those state comparisons back in, but this time above the updateAnimation() and move_and_slide()
Also change your randf_range() to randi_range() since the first one returns a float rather than an int
Not really a big issue since it will convert, but better to understand the difference
like that?
Yep, though that will never hit state == 4 since your range only goes to 3
ok and how to force this skeleton moving up?
How to force it?
umm... yeah?
just replace 4to 3?
yeah
it works
but this skeleton moves randomly without pauses
I am not understanding your intention. You code is randonly selecting a state between 0 and 3. You are also checking if the state is 4, which it will never be.
I know
Therefore you would want the range between 0 and 4. Is that right?
yep
well...
I already fixed that skeleton won't go up
now I want to force this skeleton to take break after walk
Alright, a simple suggestion is to create a simple routine
I will see if I can make something...
okay
extends CharacterBody2D
enum State
{
Idle,
Dead
}
var _state := State.Idle
func _ready():
# Start off the idle routine
_idleRoutine()
func _physics_process(_delta):
updateAnimation()
move_and_slide()
func _idleRoutine():
# This will run forever until the ai state changes
while _state == State.Idle:
# Choose a random movement
_chooseIdleMovement()
# Set the time for this movement
var duration := randi_range(1, 4)
# Wait out that timer
await get_tree().create_timer(duration).timeout
func _chooseIdleMovement():
velocity = Vector2.ZERO
var moveState := randi_range(0, 4)
match moveState:
1:
velocity.x = 100
2:
velocity.x = -100
3:
velocity.y = 100
4:
velocity.y = -100
So basically, when the script starts, it will run the _idleRoutine() which is a loop that will go forever as long as _state is set to State.Idle
The routine will pick a movement to do (including standing still if the moveState is 0) and then will choose a random duration in seconds for it to do that movement. You can set that to whatever timings you want or include some @export variables for it too