How can i use hero velocity? Or what's wrong with velocity?

What am I doing wrong trying to put my hero.velocity as condition for doing something?

Thanks! I deeply appreciate your help)
the level is https://codecombat.com/play/level/a-fine-mint

``````# Peons are trying to steal your coins!
# Write a function to squash them before they can take your coins.

def pickUpCoin():
coin = hero.findNearest(hero.findItems())
if coin:
if hero.velocity<20:
self.say("Whoa!")
hero.moveXY(coin.pos.x, coin.pos.y)

# Write the attackEnemy function below.
# Find the nearest enemy and attack them if they exist!

def attackEnemy():
enemy = hero.findNearest(hero.findEnemies())
if enemy:
hero.say(self.velocity)
if self.velocity:
self.say("message")
while True:
#attackEnemy() # ∆ Uncomment this line after you write an attackEnemy function.
pickUpCoin()
attackEnemy()
``````

I think it is in the if hero.velocity you have to have spaces like this hero.velocity < 20:

Thank you. But it doesn’t help. Ok, let’s change the code. And add spaces)

``````# Write a function to squash them before they can take your coins.

def pickUpCoin():
coin = hero.findNearest(hero.findItems())
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)

# Write the attackEnemy function below.
# Find the nearest enemy and attack them if they exist!

def attackEnemy():
enemy = hero.findNearest(hero.findEnemies())
if enemy:
hero.say(self.velocity)
if self.velocity:
self.say("message")
if hero.velocity < ({x: 0.00, y: 0.00, z: 0.00}):
self.say("Whoa!")
hero.attack(enemy)
while True:
#attackEnemy() # ∆ Uncomment this line after you write an attackEnemy function.
pickUpCoin()
attackEnemy()
``````

How we can make our hero say “Whoa!” ?

try using self in the

part

I deed it before and it doesn’t help. Actually I use both `hero` and `self`, now it doesn’t make difference.

or try putting

``````if self.velocity!=({x: 0.00, y: 0.00, z: 0.00}):
hero.say('Whoa!')
``````

try this sorry

``````if self.velocity!=({'x': 0.00, 'y': 0.00, 'z': 0.00}):
hero.say('Whoa!')
``````

Oh my God! Shame on me. Thank you very much it worked)

no problem! BTW it feels good to help.

Just now i’m busy but velocity is a vector

put in while True

``````console.log(hero.velocity)
``````

result

1 Like

Thanks a lot! I just was misreading examples/hero.outputs(

({x: 0.00, y: 0.00, z: 0.00}) in output != ({x: 0.00, y: 0.00, z: 0.00}) in code. I forgot ""s.

You can also write {x: a, y: b, z: c} as Vector(a, b, c). Just a tip to make code a little easier to write.

1 Like

@xython, @Chaboi_3000 thank you very much)
But my brain is gonna blow up)
I’m afraid I didn’t get the conception of velocity. I always thought about it like of speed. For example hero.maxHealth in most cases is the whole HP quantity at the start of the round, and hero.health - is the HP quantity at the very exact moment of the battle. So I supposed the same for hero.maxSpeed and hero.velocity.
After some time of experiments I made a conclusion that I was wrong. But obviously velocity is also is not a 3-D position of hero. Description says about meters per second, but why then it includes 3 numbers?

And in Vectors menu there is nothing close to velocity (or I didn’t found it). It helped me much with distances, some functions etc., but hero.velocity is still a mystery to me

P.S.

Thanks I love tricks that helps simplify and shorten code)

Technically CoCo works in 3-D, with a 3rd z-coordinate that goes upward. An example of this mechanic is when Okar stomps, units don’t go sideways but are launched upwards. This is because of the use of z-coordinates. If I’m not wrong, the Vector obtained from velocity is the direction you are going to per second, so if you are just walking north, and your hero speed is 14m/second, then your velocity will be Vector(0,14,0). If you are being launched from things like Okar’s stomp, then the third value(z), will change dynamically.

2 Likes

Yep, it’s especially well seen while mine-jumps)
Thanks for explanation!

I postponed asking this question for more than 2 years.
This is excerpt from Nick’s code for Crag Tag:

``````penderFuturePos = {"x": pender.pos.x + pender.velocity.x * 0.5, "y": pender.pos.y + pender.velocity.y * 0.5}
self.move(penderFuturePos)
#self.move(penderFuturePos)
penderFuturePos = {"x": pender.pos.x + pender.velocity.x * 0.5, "y": pender.pos.y + pender.velocity.y * 0.5}
dx = penderFuturePos.x - self.pos.x
dy = penderFuturePos.y - self.pos.y
jumpTo = {"x": self.pos.x + dx * 100, "y": self.pos.y + dy * 100}
``````

The offending part is pender.velocity.
In which cases does the velocity work? Should we ask Nick?

Well I told I didn’t understand velocity stuff… And with explanations the idea became more clear, but… Still it’s some kind of black magic to me I can’t master(
F.e.

``````while True:
self.say(self.velocity)
if hero.velocity!={"x":100.00, "y":200.00, "z":300.00}:
self.say("Whoa!")
``````

Works fine.
But

``````while True:
self.say(self.velocity)
if hero.velocity=={"x":0.00, "y":0.00, "z":0.00}:
self.say("Whoa!")
``````

-doesn’t.

As well as

``````while True:
self.say(self.velocity)
if hero.velocity<{"x":1000.00, "y":1000.00, "z":1000.00}:
self.say("Whoa!")
``````

I just can’t make Anya say “Whoa” in this cases.