- Home /
Blob Shadow effect using raycast on a jumping object (2D)
I have a player that jumps on platforms. I need a simple blob (circle) shadow under the player. I could've made a child object with a pre-made image of a shadow and attached it to the player, but the player can jump, so this won't work. Can I achieve this using Raycast? Enable the shadow object only when the raycast hits a 2DCollider and enable it at the end position where the raycast hit? If yes, then how can I do that? Thank You!
Answer by sparkzbarca · Jan 21, 2018 at 05:53 AM
raycasthit _info;
ray _ray;
_ray.direction = //direction;
_ray.origin = player.transform.position;
physics.raycast(ray, out _info);
gameobject Shadow;
Shadow.transform.postion = _info.point;
Shadow.transform.lookat(- _info.normal);
that look at will make it look right at the wall or whatever it just hit.
pseudo code so possibly minor mistakes.
Doesn't work! Giving a lot of errors. Used this ins$$anonymous$$d but it's still not working!
public class Raycasting : $$anonymous$$onoBehaviour
{
public GameObject shadow;
public GameObject player;
private void Update()
{
RaycastHit hit;
Physics.Raycast(transform.position, -Vector3.down, out hit);
shadow.transform.position = hit.point;
shadow.transform.LookAt(hit.normal);
}
}
Answer by Tortuap · Sep 17, 2020 at 02:05 PM
Based on SonicDirewolf version I wrote this, that works well in 3D :
public class RaycastedShadowImposter : MonoBehaviour
{
public float raycastLength = 10;
public float upDecalFromFloor = 0.05f;
private void Update ()
{
Ray ray = new Ray ( transform.position + ( Vector3.up * raycastLength ), Vector3.down * raycastLength );
if ( Physics.Raycast ( ray, out var hit ) )
{
transform.position = hit.point + ( Vector3.up * upDecalFromFloor );
transform.LookAt ( transform.position + hit.normal );
Debug.DrawLine ( hit.point, hit.point + ( hit.normal * 5 ) );
}
}
}
To put on a GameObject that contains a SpriteRenderer.
Your answer
