Character Movement

Hello,

I have created a simple maze with a cone that moves throughout. Right now I am using W:move forward, A:rotate left, S:move backward, and D:rotate right. What I would like to do is have the ‘face’ of the cone track the mouse and use ‘WASD’ for movement only (no rotation). In other words, you would ‘strafe’ around the mouse. This control scheme would be similar to what you would expect from a 3rd-person shooter. It is my understanding that this cannot be done in the GUI and requires a python script. I’ve been trying to get it to work by modifying different examples that sound similar but have had no luck (many examples seem to be using deprecated objects and methods).

I would greatly appreciate some help on this issue, either in the form of snippets of example code or a reference to a working article. After many revisions I’m pretty much back to square one:

print("start")
cont = GameLogic.getCurrentController()#store controller as 'cont'
player = cont.owner#store object as 'player'
mouse = cont.sensors["mouseTest_S"]#store sensor as 'mouse'
#rotX= cont.actuators["mouseTest_AX"]#store actuator as 'rotX'
#rotY= cont.actuators["mouseTest_AY"]#store actuator as 'rotY'

print(mouse.position[0],mouse.position[1])#print x,y of mouse

print(cont)#print controller "mouseTest_C"
print(cont.sensors)#print sensor
print(cont.actuators)#print actuator

print(player.position)#prints player position


# Get the actuators
posX = cont.actuators["rotX"]
posY = cont.actuators["rotY"]

#???somehow rotate 'player' object with actuators so face is always pointed toward mouse.position[0],mouse.position[1]???

print("end")

Sorry if this question has been asked before; I couldn’t find a similar post elsewhere or working instructions. This is my first time doing any movement at all, so I apologize if I’m missing something simple. Thanks in advance!

–UPDATE
adding:

tmp=mouse.position

lClick = cont.sensors["mouseTest_S"]
if lClick.positive:
    print("clicked")
    player.position = [tmp[0]/100,0,tmp[1]/100]

makes it so that my cone will shoot to the clicked spot, and its wildly inaccurate. I think this is a step in the right direction but I’d like it to just look at my mouse though… and use the same scale (not sure why mouse position has such higher numbers–hundreds instead of tens).

Split that in different tasks:

  1. object (empty) at location under the system mouse cursor see How to: convert mouse cursor position to 3D Space position
  2. let your player trackTo the object from 1.
  3. apply according motion to the player.

You will see each task is pretty simple. (You need python for 1. only.)

I’ll get to work deciphering this right away. It looks like you’ve pointed me in a good direction. Thanks again for the reply!

Monster,

Thank you so much for replying to my post. I was hoping to get some clarification on your instructions. You said:

Split that in different tasks:

  1. object (empty) at location under the system mouse cursor see How to: convert mouse cursor position to 3D Space position
  2. let your player trackTo the object from 1.
  3. apply according motion to the player.

NOTE: I created a cube primitive (‘the player’) and a mesh plane (‘the ground’).

I interpreted your instruction as meaning:
“1. Study this link to convert the mouse cursor position to a position in 3D space.”

I followed that link and applied the following logic to the mesh plane:
Sensor:Mouse:Movement&&Sensor:Mouse:Mouse Over Any –> Controller:Module:S2A.hitPositionToPosition –> Actuator:Movement

I’m quite certain the Acuator in this case is wrong, but I am not sure what I am supposed to… actuate? Does “Any actuator at the object to be placed at the hit position” mean that the actuator is not important on this object? I do not quite understand what you mean. Next:

“2. Let the player (cube) track the position on the object from 1 (plane), and change its own location accordingly.”

Currently my Cube has the following (surely incorrect) logic associated with it:
Sensor:Always –> Controller:Module:S2A.trackTo –> Actuator:Motion

So at this point, I know that I am successfully converting the position. This is because the plane moves when I touch it. Obviously this is wrong, as the cube should be moving. But I do not understand how to associate the cube with the plane and I dont understand what the actuators should be doing? My apologies for the confusion; I’m still quite new at this. Any further insight you could lend would be most appreciated and thank you again for your help so far.

Have a lovely day

The actuator will not get activated by this controller. Any actuator will be correct.
It is used to identify the object to be placed at the hitPosition of the sensor. You can for example position [several] other objects in one step just by connecting their actuators to this controller. You can use an existing actuator, be aware it will not be activated by the controller (that is the reason, why I like to name them “dummy” ;)) but it can be activated by other controllers.

For further explanation lets call this object “target”. Target must have an actuator that is connected with the Python controller. This will place it at the hitPosition under the system mouse cursor.

You do not need the S2A at this point. It does not even has an Entry point called “trackTo”.
You simply use an editObject/TrackTo Actuator to track your object to the object “target” that was set with task 1. Lets call this object “tracker”.

That means you have two objects:

  • “target” => at a position under the system mouse cursor
  • “tracker” => tracking target = tracking the position under the system mouse cursor

now you can move the “tracker” relative to its local position

I hope it helps

Thank you again for your help! I see that your script is pretty ingenious in its simplicity! Pardon me for bugging you even more but it looks like S2A.py is a python script (duh!), and each function that you have defined is a ‘module’ in Blender-speak. When you set the controller to use Module:S2A.hitPositionToPosition, you are saying, “Hey Blender, look in S2A.py for the function hitPositionToPosition and execute what you see.” Does this sound reasonably accurate? Also, are the other scripts (mouse.py, for example) doing anything? And how do you use the ‘show’ function in mouse.py:

def show(cont):
	import Rasterizer
	Rasterizer.showMouse(1)

I was trying to print the mouse with this function so I could see it during the ‘game’ but am not sure where to put it! I kind of cheated by putting the contents of the ‘show’ function right in the hitPositionToPosition ‘module’. That works, but I’m sure you have a cleaner method… after all, why go to all the trouble to make mouse.py! Haha

Oh yes, back to the point of this thread… I have almost gotten a working demo based on your sage advice. At this point, the “tracker” is influenced by the “target”. Great! The only problem is, the “tracker” spins when I move my mouse on top of the “target”… and the “target” shoots toward the camera when I move the mouse as well.

Is this expected? What am I messing up?

“TARGET”
Sensor:Mouse:Movement&&Sensor:Mouse:Mouse Over Any --> Controller:Module:S2A.hitPositionToPosition --> Actuator:Movement(“dummy”)
“TRACKER”
Sensor:Always --> Controller:And --> Actuator:Edit Object:Object=cube.001,Time=0,3D=false

Thank you again and again!