How to make an object face the direction of the mouse on screen.

Hello, I have been searching with no luck on how to get an object to face where ever the player points on the screen with the mouse. i have created a top down view game were the player walks with w,a,s,d, i would like him to shoot, but i want him to shoot in the direction of where the mouse pointer is located on screen. ( If that makes sense ):spin:

If anyone knows how i can achieve this please let me know :slight_smile:

Thank you for any help.

Kailan.

Hello
do a search in the “Resources” forum for “Point’n’Click” demos/games, maybe useful?!
Bye

i’ll have a look

rotate.blend (133 KB)It tracks the player to the position of an empty, which is at the mouse hit position,
you need:

  • the mouse to always be over objects
  • an empty with a mouse over any sensor called “mouse”
  • mouse sensor true pulse

this code:


controller = GameLogic.getCurrentController()
owner = controller.owner
import Rasterizer
mouse = controller.sensors["mouse"]
Rasterizer.showMouse(1)
owner.position = mouse.hitPosition
print mouse.hitPosition

Wow thanks a lot guys :slight_smile:
Ill see how it go’s

Thank you agoose77 this is exactly what i was looking for.

i have seen you ask another question about parenting to mouse.
You can use my example of the rotate, but either center the empty in the cube and parent them, or add another script to the cube:


controller = GameLogic.getCurrentController()
owner = controller.owner
scene  = GameLogic.getCurrentScene()
objects = scene.objects
empty = objects["OBEmpty"]
owner.position = empty.position

Yea i am using a code at the moment and I found if I parent the object to the empty it works fine =)

import GameLogic
cont = GameLogic.getCurrentController()
own = cont.owner
 
mouse = cont.sensors["mouse"]
 
if mouse.positive:
    
    own.localPosition = mouse.hitPosition
    

This seems to work for me in 2.5 :D.

Oh by the way is there a way to make the game into an .exe or is that still incomplete, if you don’t mind me asking. Thanks heaps mate :slight_smile: