Jump to content

Walk and Run Speeds Reversed


Recommended Posts

A follower creature I made seems to work OK, but the running and walking speeds I set are reversed.
At least, I think that's the case, since it starts walking at its running speed and then switches to walking speed when my distance from it grows.

Here are the relevant parts of the code:

Prefab

local function init_prefab()
	-- ...
	inst:AddComponent("follower")
	-- ...
	inst:AddComponent("locomotor")
	inst.components.locomotor.walkspeed = 5
	inst.components.locomotor.runspeed = 10
	-- ...
	return inst
end

Stategraph

local states =
{
	-- ...

	State{
		name = "walk_start",
		tags = {"moving", "canrotate"},
		
		onenter = function(inst) 
			inst.AnimState:PlayAnimation("walk_pre")
		end,
		
		events =
		{   
			EventHandler("animover", function(inst) inst.sg:GoToState("walk") end ),
		},
	},
	
	-- This handles the running state.
	State{
		
		name = "walk",
		
		-- Our running animation is also safe to be rotated.
		tags = { "canrotate", "moving" },
		
		onenter = function(inst, playanim)
			
			-- When entering this state, we now play a looping run animation.
			inst.AnimState:PlayAnimation("walk_loop", true)
			
			-- Tell the locomotor that it is ok to start running.
			inst.components.locomotor:WalkForward()
		end,
	},
	
	State{
		name = "walk_stop",
		tags = {"canrotate"},
		
		onenter = function(inst) 
			inst.components.locomotor:StopMoving()
			inst.AnimState:PlayAnimation("walk_pst")
		end,
		
		events=
		{   
			EventHandler("animover", function(inst) inst.sg:GoToState("idle") end ),
		},
	},

    State{

        name = "run",
        tags = {"moving", "running", "canrotate"},

        onenter = function(inst)
            inst.components.locomotor:RunForward()
            inst.AnimState:PlayAnimation("walk_loop")
        end,
		
		ontimeout = function(inst)
            inst.sg:GoToState("walk")
        end,
		
		events=
        {   
            EventHandler("animover", function(inst) inst.sg:GoToState("walk") end ),        
        },
	
    },
	
-- ...
}

local event_handlers =
{
    EventHandler("locomote", function(inst) 
        if not inst.sg:HasStateTag("busy") then
            local is_moving = inst.sg:HasStateTag("moving")
            local wants_to_move = inst.components.locomotor:WantsToMoveForward()
            if not inst.sg:HasStateTag("attack") and is_moving ~= wants_to_move then
                if wants_to_move then
					local should_run = inst.components.locomotor:WantsToRun()
					if should_run then
						inst.sg:GoToState("run")
					else
						inst.sg:GoToState("walk")
					end
                else
                    inst.sg:GoToState("idle")
                end
            end
        end
    end),
}

Brain

local MIN_FOLLOW_DIST = 0
local MAX_FOLLOW_DIST = 6
local TARGET_FOLLOW_DIST = 6
-- ...

-- In the OnStart function, under a root PriorityNode
Follow(self.inst, function() return self.inst.components.follower.leader end, MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST, true),

 

Of course, I could just reverse the values of the walking and running speeds, which is what I've done for now in order for it to work decently, but that's clearly not the best solution here...

Any help would be appreciated.
Thanks in advance. ^_^

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...