Bad argument #2 to '__add' (vector3 expected, got number)

Hello all

i have this simple source code but im keep getting :
ERROR:SCRIPT: main/hero.script:30: bad argument #2 to β€˜__add’ (vector3 expected, got number)
stack traceback:
[C]:-1: in function __add
main/hero.script:30: in function <main/hero.script:28>

The source code , i think i done every thing right , looks like the fixed_update(self,dt) β†’ position + self.velocity * dt
is not vector although i did set it to vector in init

local DIRECTION_RIGHT=5
local DIRECTION_LEFT=-5
local BASE_VELOCITY = 500

function init(self)
	msg.post("#","acquire_input_focus")
	self.velocity = vmath.vector3(0,0,0)
end


function walk(self)
	
	self.velocity = BASE_VELOCITY * self.direction
end

function flip(direction)
	sprite.set_hflip("#sprite",direction < 0) 
end

function animate(action)
	if action.pressed then
		sprite.play_flipbook("#sprite","run") 
	elseif action.released then 
		sprite.play_flipbook("#sprite","idle")
	end	
end

function fixed_update(self,dt)
	local position = go.get_position();
	position = position + self.velocity * dt
	go.set_position(position) 
end

function on_input(self, action_id, action)

	animate(action)
	self.direction = (action_id == hash("right")) and DIRECTION_RIGHT or DIRECTION_LEFT 
	
	walk(self)
	flip(self.direction)
end

Thanks

Your direction variables are incorrect. In walk(), you set self.velocity to BASE_VELOCITY (a number) multiplied by a direction (also a number). Your directions should probably be:

local DIRECTION_RIGHT=vmath.vector3(1,0,0)
local DIRECTION_RIGHT=vmath.vector3(-1,0,0)

I changed them from 5 to 1 because normally you would expect a speed variable to determine the magnitude (what you call BASE_VELOCITY), whereas a direction vector should be normalised (i.e. the length of it is = 1).

2 Likes

Thank you its strange
in this tutorial i follow he dosnt set it to vector

In that tutorial, it’s:

self.velocity.x =

Not:

self.velocity = 
2 Likes

Thanks !