QUESTION: UPBGE fake player shadow

i want to make a shadow like this, but i don’t know how to do it.


How do i make a plane that follows the character only in the ground?

  1. slap blob shadow texture on a plane.
  2. set shadow xy position to character xy position.
  3. raycast from the character position downwards.
  4. if hit set shadow z world position to hit position. else set shadow invisible.

untested example code.

from bge.logic import getCurrentController, getCurrentScene
from mathutils import Vector
own = getCurrentController().owner
blob = getCurrentScene().objects["shadow"]

blob.worldPosition.xy = own.worldPosition.xy

vecTo = own.worldPosition.copy() - Vector([0,0,10])
hitObj, hitPos, hitNormal = own.rayCast(vecTo)


if hitObj:
    if not hitObj.invalid:
        blob.worldPosition.z = hitPos[2]
        blob.visible = True

else: blob.visible = False

additionally you can take the distance between the character and the hit position to calculate a scaling factor for the shadow (make it smaller the farther away the character is).

itd be something along the lines of max(1, 1/(0.1 + distance)) if im not getting confused here.

Thanks, it worked like you said, but my shadow is not following correctly the player:
Shadow_Test.blend (543.2 KB)
sorry, i’m an noob and i just starded to use Blender recently !

unparent the shadow. the script already handles moving the shadow around.
run the script on the character, not the shadow.
make the shadow no collision on the physics panel. if left on static, the character will collide with it.

raycasting can be very expensive, there is a way you can selectively apply shadows to materials, uncheck cast on the mario material, and have an invisible sphere cast the shadow.

ill be back shortly after i have a look at my file to remember

edit: slopes could cause visual issues on your shadow blob object, applying an align axis to vector along face normal will help, but not perfectly

BLEND
shadowblob.blend (381.2 KB)

thank you Liebranca. Now it worked, the shadow follows the player perfectly!