Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Determining a Velocity/Speed?

Asked by 9 years ago

Right. Im creating a animation script for a RPG enemy, (and I know it is not correct), and that I cant figure out what I need to find out the speed/or at least how to tell whether the enemy is moving, so when he is moving he "runs" and when he is not, he "Stands". If you use a free model script, the animation for a RPG enemy works fine, in solo mode. However when going to normal (multiplayer) mode it decides to not work. idk if this is to do with servers/clients etc, because I don't understand them. Is there something else I need to do? :

function onRunning()
    if isSeated then return end

    if script.Parent.Torso.Velocity > (0,0,0) then -- A single value wont work
        pose = "Running"
    else
        pose = "Standing"
    end
end

Thanks ~Bubs

2 answers

Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

If you're using the Running event, you can receive the speed of the character by its argument.

Humanoid.Running:connect(function (speed)
    if speed > 20 then
        print("Wow, you're fast!")
    else
        print("Wow, you're slow.")
    end
end)

You could also calculate the speed by using the HumanoidRootPart velocity's "x" and "z" components with Pythagorean Theorem.

HRP = script.Parent.HumanoidRootPart

function PythagoreanTheorem(a, b)
    local solution = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
    return solution
end

function Example()
    if PythagoreanTheorem(HRP.Velocity.x, HRP.Velocity.z) > 20 then
        print("Wow, you're fast!")
    else
        print("Wow, you're slow.")
    end
end

Why not use the Torso's velocity components? Because, in my perspective, animations can make the velocity of the torso go faster.

0
Great Stuff, Thanks! :) Bubbles5610 217 — 9y
0
Ah. This Actually Caused a Problem :'( . Like i said earlier, this only works in edit mode, solo. now in the actual game? Any Ideas? ( Running event) Bubbles5610 217 — 9y
0
Any errors in the output? Spongocardo 1991 — 9y
0
none at all - i would hapily uncopylock if you wish? Bubbles5610 217 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

If your enemy has a Humanoid in it, you can use the Running event of the Humanoid to determine whether they are running or not.

The running event has a single number parameter, speed. This variable tells you how fast the humanoid is running when the event is triggered.

The running event is also useful to you because this event is triggered when the player stops moving too, where the speed parameter becomes 0.

For your script above, you can do the following to accomplish what you're doing:

script.Parent.Humanoid.Running:connect(function(speed) --Change the path so it points to the humanoid of the enemy.
    if isSeated then return end --I suppose you need this so I kept it in just in case.
    if speed > 0 then --If the enemy is running then set the pose.
        pose = "Running"
    else
        pose = "Standing"
    end
end)
0
Don't you just hate when people ninja you? Sorry, Red. Spongocardo 1991 — 9y
0
Ah. This Actually Caused a Problem :'( . Like i said earlier, this only works in edit mode, solo. now in the actual game? Any Ideas? Bubbles5610 217 — 9y

Answer this question