- Home /
(iOS) Plane shadow that follows object on flat terrain - Translate but not Rotate.
I have a sphere that moves on a flat terrain by finger swipping. The sphere does not only translate but rotates too.
I was planning to use a plane at ground level with a shadow texture on it. Then parent my sphere to it. Then my "shadow" would follow my sphere like a real shadow (and I'd only be charged 1 draw call in comparison to using a blob shadow - that's my goal).
Of course my "shadow" rotates as my sphere rotates while translating.
I thought of using the same script that controls the sphere translation & rotation on the "shadow", after having removed the rotation-relevant lines of code. So the "shadow" would have a parallel behavior to the sphere, without the rotation.
Then I thought that maybe there is another way, less cpu demanding (supposing that using this 2nd script is a small extra burden for the cpu - and I am trying to avoid that as much as I can).
Any ideas on how to make the "shadow" follow the sphere without rotating, would be deeply appreciated. :-)
Answer by Eric5h5 · Dec 17, 2010 at 10:05 AM
Use a script on the shadow that just follows the sphere:
var transformToFollow : Transform; var offset = Vector3.zero; private var myTransform : Transform;
function Start () { myTransform = transform; }
function LateUpdate () { myTransform.position = transformToFollow.position + offset; }
Thank you very much Eric5h5! That did it! In the meantime I also used the script that moves the sphere on the shadow (after removing the rotation-relevant lines of code) and it worked. It's a 100 line code basically (the sphere rotation is a bit sophisticated and I use some iTween classes hence the big length) and uses swipes as I mention in my Question. Using the code you nicely suggested, does the game become (at least a bit) "lighter" for the CPU or is the difference negligible and it doesn't make a difference CPU-wise? :-)
The script is nearly the $$anonymous$$imum possible CPU usage. You could potentially remove "offset" if you were using Vector3.zero for it anyway. I couldn't say what the difference is without seeing your script, but if you're getting input and applying it to the shadow, that's added CPU time right there. On mobile devices, it's important not just to consider the end result as frames per second, but also how much work you're making the CPU do, since everything you do drains battery, and you want to $$anonymous$$imize that as much as possible.