#My enemy won't walk randomly until it'll be touched and reaches bottom end.

1 messages · Page 1 of 1 (latest)

safe magnet
quaint trout
#

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

safe magnet
#

this skeleton is still being pushed by the player

quaint trout
#

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

safe magnet
#

like that?

#

but it's also doesn't changes

quaint trout
#

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

safe magnet
#

hmm... which should I replace state == 0, state == 1, state == 2, state == 3, and state == 4?

quaint trout
#
func _physics_process(_delta):
    velocity.x = 100
    updateAnimation()
    move_and_slide()
#

For now just do this

#

Just trying to debug the issue for now

safe magnet
#

yeah, it works

#

this skeleton moves straight right

quaint trout
#

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

quaint trout
safe magnet
quaint trout
#

How to force it?

safe magnet
#

just replace 4to 3?

#

yeah

#

it works

#

but this skeleton moves randomly without pauses

quaint trout
#

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.

quaint trout
#

Therefore you would want the range between 0 and 4. Is that right?

safe magnet
#

yep

#

well...

#

I already fixed that skeleton won't go up

#

now I want to force this skeleton to take break after walk

quaint trout
#

Alright, a simple suggestion is to create a simple routine

#

I will see if I can make something...

safe magnet
#

okay

quaint trout
#
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

safe magnet
#

omg, you're lifesaver, thank you so much!

#

anyway this problem solved