# Rotate an object along face normal in the bge

I have an empty that is at the center of the screen, it move to whatever you point at. An explanation on how to do so can be found here.

I am looking to have a plane or empty always face outwards of whatever object it is colliding with. If this can't be done with an empty, I would be fine with a plane.

Here's a picture of what I don't want: (like this but in-game)

Here's a picture of what I do want: (like this but in-game, used wireframe as you cannot see in textured) See the plane is rotated to match the cube's face.

I am basically looking for the empty/plane (preferably empty) to face the outside of whatever it is colliding with.

• I assume you want to get the colliding face's normal so you can add a bullet hit or some other effect, right? – David Sep 29 '15 at 16:12
• @David♦ Yes, that is correct. – blackhole Sep 30 '15 at 1:11
• ..."according to an object's face normals" would be a more accurate title. If I understand what you're after. – Mentalist Sep 30 '15 at 3:42

Set up the logic bricks as pictured above.

This script will move an object (the cone) to the location of ray.hitPosition and then rotate it to point along the ray.hitNormal using alignAxisToVect().
For this example, the cone is pointing along the positive z axis. If it was along a different axis, I would have to pass in the second parameter to alignAxisToVect().

import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()

ray = cont.sensors["Ray"]
cone = scene.objects["Cone"]

if ray.hitObject and cone is not ray.hitObject:
cone.worldPosition = ray.hitPosition
cone.alignAxisToVect(ray.hitNormal)


Example gif, showing the cone moving and rotating to match the underlying geometry in the game engine.

### Blend File

• There is no need to arctan anything. You can use KX_GameObject.alignAxisToVect(ray.hitNormal) which aligns the object's z-axis along the normal. – Monster Oct 6 '15 at 8:48
• @Monster thanks for pointing me to that function, much simpler. edited my answer. – David Oct 6 '15 at 21:02