We need to start with a object that tracks to the mouse. Before we can do that, we need something for the object to track along. for this we will use a highly modified cylinder that resembles an inverted cone.
We need a shape like this so that the player can not ever move the mouse outside of the tracking zone.
Now the higher your mouse is, the further away it the mouse will be.
Once we have the basic setup, we can get the mechanics working.
By basic setup, I mean, a player, a camera, a tracking object, a subdivided plane (to show movement), and a cylinder. (The cylinder is irrelevant now, it can be no-collision object and be invisible. It only needs to be parented to the player) I am going to name the player Space Ship Cube and the tracking object Fly_To_Target We also need the camera slow parented to the player. the giant tracking field also needs to be parented to the player
(still named Space Ship Cube)
Although I always try to avoid python if at all possible, sometimes it can not be avoided, and this is one of those times. we must add a script named Trackto.py
from bge import logic
scene = logic.getCurrentScene()
cont = logic.getCurrentController()
mouse_over = cont.sensors["MouseOver"]
if mouse_over.positive and scene.objects["Fly_To_Target"]['ActiveYesNo?'] :
tracker = scene.objects["Fly_To_Target"]
tracker.worldPosition = mouse_over.hitPosition
tracker.worldPosition.z += 1
There is some thing that this script references that currently does not exist. We need a Boolean property on Fly_To_Target named ActiveYesNo?
Of course none of this helps us if there is nothing to run the script. (don't worry, were going to make that now)
Now we if we press play it should look like this. (the plane has been hidden to show the tracking object and the field it runs along.)
Now we have a wonderful object tracking to the mouse. This is good, because now we can have the player (yes, It is Still named Space Ship Cube) track to it
Getting Space Ship Cube to track to Fly_To_Target is not hard at all.
For now, this is all we need on the player. All this does is make it so when the Boolean property Drive? is true, the cube will move in the direction of our tracker object named Fly_To_Target
Now the Space ship cube will always fly to the the object Fly_To_Target we have tracking to the mouse.
There is one thing we need to change before we can test it. Because we are using linear velocity, there will be a significant overshoot problem. for details on why, view this question.
If you do not care why, and just want the problem fixed, set the damping on the Translation to .8
Now we can test it and it should work.
This is obviously not what we want, but it is very close.
We can start by setting the Boolean property Drive? to false, and adding a mouse sensor and property actuator to set Drive? to true, because, when it is true, our space ship (Space Ship Cube) flies to object Fly_To_Target It is very important that Drive? is False when the game starts, or it will begin flying to Fly_To_Target as soon as the game is started.
Now if we test it, we should expect Space Ship Cube to remain still until we click
Unfortunately we still have the problem of Space Ship Cube indefinitely trying to reach Fly_To_Target. Do not worry, There is an easy fix for this. We can simply use that same mouse sensor to assign the Boolean property ActiveYesNo? to False
Now when we click, Fly_To_Target will no longer move.
That only fixed half of our problem. Space Ship Cube Still flies indefinitely. We still need something to make the cube stop moving once it reaches Fly_To_Target There is no need to worry, because I would not be writing this answer If I didn't know how to fix this.
Remember the cylinder that we added earlier and strangely parented to Space Ship Cube? we will now make use of that. Until now it has been invisible, because we didn't need to see it. However, for illustration purposes, we can now make it visible again. While we are in the physics tab, we need to change a few settings.
I did not want to draw big blue arrows over the entire thing, but all of the settings there are important. It is very important that the physics type is set to sensor (detects collisions with static and dynamic objects but not other sensor objects) It is also important that the check box Compound is checked. this will basically make the collision bounds of the cylinder part of Space Ship Cube.
Of course for this to work the same check box needs to be checked on Space Ship Cube
Now we have a wonderful collision sensor! - Whoopee. Now we need it to do something. We are actually going to use it to detect a collision with Fly_To_Target
We can do this by placing the sensor on Space Ship Cube
If we test it, Space Ship Cube should stop once it reaches Fly_To_Target
Unfortunately, Fly_To_Target will not move again because we assigned it's Boolean property ActiveYesNo? to false, essentially deactivating it. If we want it to work, we need to assign it to True
This is easy enough to do by adding one extra logic brick.
Now we can test it and everything should work.
Yay! everything works!
Until now I have been using a subdivided plane to show movement.
In space however, there is no floor. so we can hide the plane. Remember, it is a no-collision object so it is effectively gone now. (Gravity is set to 0 so the objects will not fall)
If you had any trouble following that, (Don't worry, this even confused me the first time I read through it) you can always look at/take apart the blend file.