- Home /
Transforming 2D object with respect to UI
So I've been trying to make this 3d isometric game where I have set my camera rotation as x: 33.2 y:44.5, which means that when I move something along the x-axis, it also changes the values of my y-axis and z-axis after scripting. In easy words, I want to move my 2d sprite horizontally with respect to the UI instead of considering the world axis. Is there a possible solution to move it like that?
// Update is called once per frame
void Update()
{
transform.position += new Vector3(beatTempo * Time.deltaTime, 0f, 0f);
}
.
Answer by Namey5 · Aug 03, 2020 at 08:50 AM
If you want to move something in relation to the camera, then you can simply transform the movement vector by the camera's rotation;
private Camera cam;
private void Update ()
{
//We don't want to access Camera.main every frame, so get it once and store it
if (!cam)
cam = Camera.main;
//Then, just multiply the movement vector by the camera's rotation to rotate it. Now, it will be defined relative to the camera's rotation.
transform.position += cam.transform.rotation * new Vector3 (beatTempo * Time.deltaTime, 0f, 0f);
}
I basically tried this one and it says that the cam." rotation" doesn't exist. Something like this
$$anonymous$$y bad, forgot to reference the camera's transform component. Answer is updated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004555im_/https://answers.unity.com/themes/thub/images/avi.jpg)