in an iOS project, there is a 3D object which can be rotated and moved around on screen by the user. at the bottom of the screen is a drop shadow like this:
since i want the shadow to only move along the x axis, i’m using WorldToScreenPoint on the object and update the x value of the shadow’s localPosition accordingly.
Now the problem is when i’m moving the object around, the shadow will sort of jump around in a glitchy way, depending on the speed in which the object is moved… it’s further away from its intended position when i move the object faster, and is nice and steady and where it should be when the object isn’t moved. it is a bit hard to explain so here’s another pic:
Intuitively i would say that somehow the previous frame’s values are used for some of the transform calculations, or the order in which the scripts are executed is wrong? No idea how to fix this, since i’m quite unexperienced with U3D…
here’s the script which moves the shadow:
public class ShadowAligner : MonoBehaviour
{
public Transform screenObject;
public Transform shadow;
public Camera cam;
float factor = 1;
public float y = 0;
public float z = 0;
void LateUpdate ()
{
Vector3 screenPos = cam.WorldToScreenPoint(screenObject.position);
float x = screenPos.x;
Vector3 pos = shadow.localPosition;
pos.x = map(x, 0, Screen.width, -factor, factor);
shadow.localPosition = new Vector3(pos.x, y, z);
}
float map(float value, float istart, float istop, float ostart, float ostop)
{
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
}