Gyroscope useage to move character

I’ve just recently picked this up, ran thru some tutorial and now I’ve started a new project to understand a bit better.

I’d like to make use of accelerator but not sure how to hook it in.

    if action_id == hash("move_right") or action.acc_x then
        if action.released then -- reset velocity if input was released
            self.velocity = vmath.vector3()
        else -- update velocity
            self.velocity = vmath.vector3(action.value * self.max_speed, 0, 0)
        end

Thanks

If I’m not mistaken action.acc_x and action.acc_y should be gyroscope values in horizontal and vertical direction. Aren’t you getting any values? What if you print the values?

Printed values and they are registering. Learning this as I go.

Now I try this in on_input

   elseif action.acc_x then    
        if action.released then -- reset velocity if input was released
            self.velocity = vmath.vector3()
        else -- update velocity 
       self.velocity = vmath.vector3( action.acc_x * self.max_speed,0,0)
       end
  elseif action.acc_y then  
      if action.released then -- reset velocity if input was released
            self.velocity = vmath.vector3()
        else -- update velocity     
       self.velocity = vmath.vector3( 0, action.acc_y * self.max_speed,0)

My object is just on the x plain, moving… but not on y.

That is because action.acc_x will be set so it branches into the first of your cases and will never hit the “elseif action.acc_y” one. Try changing it either so it is two separate if statements or just change the velocity line to

self.velocity = vmath.vector3( action.acc_x * self.max_speed,action.acc_y * self.max_speed,0)

1 Like

Johan is correct. acc_x will always be set so the first conditional will always evaluate to true. I think it makes the most sense to compact it into a single case.

Looks like this is how to move by gyro. Thanks for the help. Now I just have to bounce off some objects.

function init(self)
– this lets us handle input in this script
msg.post(".", “acquire_input_focus”)
– save position
self.position = go.get_position()
msg.post("#", “reset”)
self.max_speed = 200
– velocity of the instance, initially zero
self.velocity = vmath.vector3()
end

function update(self, dt)
– move the instance
go.set_position(go.get_position() + dt * self.velocity)
end

function on_input(self, action_id, action)
– check for movement input
self.velocity = vmath.vector3( action.acc_x * self.max_speed,action.acc_y * self.max_speed,0)
end

3 Likes