- Home /
weird bug on updating transforms
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));
}
}
Answer by MrFiveStudios · Jul 09, 2011 at 11:14 PM
So, a couple of questions: Is the drop shadow a child of another object? When you use Transform.localPosition, you are moving the object relative to its parents, versus if you used Transform.position, which would be absolute. This could explain why it is moving faster than the cube, especially it's already parented to the cube.
Secondly, why not skirt around this obstacle entirely by parenting the drop shadow to the cube, and simply explicitly set its Transform.position.y to a specific value, something like:
Transform.position = new Vector3(parent_Cube.transform.position.x,*constant y value*, 0f);
Thus only allow it to change the x based on the cube's x movements.
the shadow is a child of the background texture, which in turn is a child of the main camera. The 3D object is in its own space.. actually it's located at the origin, and what looks like moving/rotating/zoo$$anonymous$$g the object means in fact panning/rotating/changing-the-distance-of the camera (including its children) in relation to that object.
It's mainly set up like this because i get the 3D scene from someone else, my job is to make it a nice iPad app. So yeah, the 8 or so scripts which are needed are kind of arbitrarily distributed over the scene objects because it seemed like a good idea at the time :]
Anyway, if i move the object and then let go of it, the shadow will be in the right place in the end.. only during movement of the object it is offset. So geometrically it's okay, though not very elegant, i suppose?
Actually i've tried parenting the shadow to the cube at first, as this was the most obvious way to go i guess... since the object can be rotated i set the shadow's rotation to Identity on each frame, but already had the very same problem which i have now...
oh gosh... there was a stray line of code in the camera script which would translate the shadow. accepting your answer because it encouraged me looking at the scripts :]
Haha, no worries. It's always the little things that gets us :).
Your answer
![](https://koobas.hobune.stream/wayback/20220612140128im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating a football with a spiral... 3 Answers
Trying to keep GameObject (Ship) locked into visible screen space 1 Answer
Unity Car Accelerometer 0 Answers
Elevator Script ? 2 Answers
How to transform points from world to camera space and back again. 1 Answer