How to set local linear velocity of an object related to the camera view(code inside)

Hey guys. My knowledge of Python is very limited.
I’ve followed one of Goran’s tutorials on mouse grab&drop with python.
I wanted to add a throw function with the rightmouse click which I did and it worked but the object flies in it’s own local coordinates and I want it to fly according to the camera view. Here is Goran’s code, modified slightly by me. The question is how to make the object fly in the direction of the viewer (camera/player).

from bge import logic
from mathutils import Vector
import fps

class Grabber:

def __init__(self, cont):
       
    self.cont = cont
    self.camera = self.cont.owner
    self.selector = self.camera.children[0]
       
    self.coltarget = self.cont.sensors["coltarget"]
    self.lclick = self.cont.sensors["lclick"]
    self.rclick = self.cont.sensors["rclick"]
    
    self.grabbed = None
    
    self.main = self.pickup
       
def pickup(self):
           
           if self.coltarget.positive:
               self.selector.applyRotation([0, 0.1, 0], True)
                
               if self.lclick.positive:
                    self.grabbed = self.coltarget.hitObject
                    self.main = self.carry
def throw(self):


    self.grabbed.localLinearVelocity = [0, 10, 0]
    self.main = self.pickup
                    
def carry(self):
    
    delta = self.selector.worldPosition - self.grabbed.worldPosition
    self.grabbed.applyForce(delta*1000)
    self.grabbed.setLinearVelocity([0, 0, 0])


    if self.lclick.positive:
        self.main = self.pickup
    if self.rclick.positive:
        self.main = self.throw

class Player:

def __init__(self, cont, body):
    self.mouse_look = fps.MouseLook(cont, body)
    self.move_wsad = fps.MoveWSAD(cont, body)
    self.grabber = Grabber(cont)


def main(self):
    self.mouse_look.main()
    self.move_wsad.main()
    self.grabber.main()

player = Player(logic.getCurrentController(),
logic.getCurrentScene().objects[“Player”])

def main():

player.main()

100 views and noone knows the answer or at least some directions? :slight_smile:

Could you upload a blend? It might help to try out a few ideas.

One thing that pops to mind is multiplying the throw vector, [0,10,0] by the cameras orientation matrix.

So your code would look like this: self.grabbed.localLinearVelocity = Vector([0, 10, 0]) * Camera.worldOrientation

Not sure if that will fix it, but worth a try haha
Ex.

I tried it, but to no avail. It says in the console that the Camera is not defined.
Here is the blend.
Grab_throw_drop.blend (680 KB)

This topic solve this problem: Grab, Drop and Throw objects with your mouse (Using Constraints)