Linear Velocity Help

In my project, when the player lifts his/her finger off the screen, I want the player to speed towards a sphere that’s in a different position from the player. But this comes with two problems. The first being that the player kinda moves off to the side and not towards the position of the sphere, and the second problem being that sometimes, the player will shoot up straight into the air and move at the same time, thus making the player fall off the map. Is there anyway to fix this???

Here’s the code I’m scratching my head on:

if touch.state == ENDED then
        b.linearVelocity = vec3(lob.x - math.ceil((lob.x - 100)/12), lob.y - math.ceil((lob.y - 100)/12), lob.z - math.ceil((lob.z - 150)/12))
        b.linearDamping = 0.4
        b.friction = 2
end

Thanks in advance :slight_smile:

@Creator27 You have to subtract the positions of the sphere and the player. That should give the amount (x,y,z) you give to the player to move towards the sphere.

@dave1707, do you mean something like this??

b.linearVelocity = vec3(lob.x - bean.x * 5, 0, lob.z - bean.z * 5)

@Creator27 Here’s an example I have in 2D.

viewer.mode=FULLSCREEN
function setup()
    a=physics.body(CIRCLE,35)
    a.x=math.random(WIDTH)
    a.y=math.random(HEIGHT/4)
    a.gravityScale=0
    target = vec2(WIDTH/2, HEIGHT-150)
end

function draw()
    background(142, 88, 38, 255)
    ellipse(target.x,target.y,30)
    moveSpr(a,target,asset.builtin.Planet_Cute.Character_Boy)
end

function moveSpr(obj,tar,spr)
    move= tar - vec2(obj.x, obj.y)   
    if move:len() > 10 then
        move = move:normalize() * 10
    end
    obj.x = obj.x + move.x
    obj.y = obj.y + move.y
    sprite(spr, obj.x, obj.y, 60)
end

@dave1707, I tried using your code in Codea, but it came up with some errors. Do you have another example you could possibly show me?

Thanks :slight_smile:

function setup()
    startPos={x=0,y=HEIGHT/2} -- start position
    endPos={x=WIDTH,y=HEIGHT/2} --end position
    duration=5; --how long it takes
end

function move()
    tween(duration,startPos,endPos,    -- time, start, end
    {easing=tween.easing.linear,loop=tween.loop.once},done) -- tween type
end

function draw()
    background(40, 40, 50)
    fill(255)
    sprite("Platformer Art:Guy Standing",startPos.x,startPos.y)
end

function touched(touch)
    
    if touch.state == ENDED then
        move()
    end
end