- Home /
Best practice for making blob shadow projector always face down?
I've come across 2 methods so far, one which involves making the projector a child of the object casting the shadow and then using this script on the projector itself:
function Update () {
var facedown = Quaternion.identity;
facedown.eulerAngles = new Vector3( 90, 0, 0);
transform.rotation = facedown;
}
Which sort of works... but seems to have weird artifacts when the parent object rotates - and the second method which involves copying the objects position information over to the projector's position information:
var objectToFollow : Transform;
var offset : Vector3;
function Update () {
transform.position = objectToFollow.position + offset;
}
I haven't tried the second method yet, but I figure the projector can no longer be a child of the main object which sounds like it could become quite messy.
Any tips?
That 2nd method ROC$$anonymous$$S!! I just place the projector into an epty game object placed at ground level with the projector above facing downwards, applied the script, assigned my character, SWEEETNESS it's simplicty at it's best!!
I want to offer this handy script for the second method, which improves it by automatically making the shadows imitate the sun angle. Just slap it on the projector and assign the variables in the inspector, and you're all set!
// ShadowFollow.js
#pragma strict
var objectToFollow : Transform; // The object you need a shadow under
var offset : Vector3; // How much to shift the projector from the object center
var lightSource : Light; // What light is "casting" the shadow
function Start () {
transform.rotation = lightSource.transform.rotation;
}
function Update () {
transform.position = objectToFollow.position + offset;
}
Very handy script, I did notice however that it casts the shadow in the wrong direction so it does not match up with my light mapped shadows, any way to fix that to make it more accurate? Also trying to figure out how to make it so as when my enemy character is destroyed that the blob shadow projector also gets destroyed because right now I get an error message: $$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
Hmm. Well, it's designed for use with a directional light, such as the sun, which means the angle of the shadow never changes. However, you CAN modify it to work with scene lights. Here's how I'd do it.
Ins$$anonymous$$d of setting the rotation in the Start() function, you'll need to set it in Update(). In addition, the rotation will not be set to the light's rotation. Ins$$anonymous$$d, you will need to calculate a vector for transform.position - lightSource.transform.position, then find the rotation of that vector, then use THAT rotation for transform.rotation.
So in the end, the rotation part would look something like:
function Update () {
var shadowVector : Vector3 = transform.position - lightSource.transform.position;
transform.rotation = Quaternion.lookRotation(shadowVector);
(... other stuff already in Update)
}
Oh, and as for your second question... I would try making the shadow part of a Prefab, including both the character and the shadow projector. When the enemy needs to die, kill the whole Prefab.
There are other ways to do this, too. For instance, you could have the Update() function include a check to see if the object is still in existence:
if (!transform)
<delete whatever>
Answer by Meltdown · Oct 12, 2011 at 04:51 PM
Instantiate the shadow casting object, then instantiate the projector seperately. It has no reason to be a child of of the object, as the rotation will cause problems.
Answer by Meltdown · Oct 12, 2011 at 04:04 PM
The second method is the way to go. Nothing messy about it :)
Answer by Alkan · Oct 12, 2011 at 04:48 PM
If I'm instantiating the shadow casting object, where would you recommend I place the projector?
Do I instantiate it along with the object at the same time - or is there a way I can nest it in with the object-prefab as a child?
,I see, if I'm instantiating the shadow casting objects, would you advise that I also instantiate the shadow projector at the same time?
or could I make a prefab that contained both?
You should post this as a comment to my answer... not as an 'answer'
Answer by austint30 · Jan 05, 2014 at 06:09 AM
I used this script for my car shadow, but the shadow does not rotate with my car!