Need help with script (changes based on cam direction)

I have this script:

        //ASSUMING CAMERA TRANSFORM FORWARD OF (0,0,1)

        Vector3 AssumedPos;

        if (self.transform.position.z > target.transform.position.z)//(THIS EVALUTAION NEEDS TO CHANGE BASED ON CAMERA TRANSFORM FORWARD)
        {
            AssumedPos =       new Vector3(
                                            self.transform.position.x,
                                            self.transform.position.y,
                                            target.transform.position.z
                                          );//(THIS RESULT NEEDS TO CHANGE BASED ON CAMERA TRANSFORM FORWARD)

            Debug.Log("using clamped assumed pos");
        }
        else
        {
            AssumedPos = self.transform.position;
        }

basically, assuming that camera is currently facing (0,0,1), this script detects if self.z has gone past the target.z, and if so it locks the assumed position.z to be the same as the target, while keeping the x and y the same.

Now I need it to be dynamic, and behave with the same general spirit of the code but the camera direction can be rotated, for example, the transform forward of the cam might be (1,0,0)

in this case the code should instead behave like this:

        //ASSUMING CAMERA TRANSFORM FORWARD OF (1,0,0)

        Vector3 AssumedPos;

        if (self.transform.position.x > target.transform.position.x)//(THIS EVALUTAION NEEDS TO CHANGE BASED ON CAMERA TRANSFORM FORWARD)
        {
            AssumedPos =       new Vector3(
                                            target.transform.position.x,
                                            self.transform.position.y,
                                            self.transform.position.z
                                          );//(THIS RESULT NEEDS TO CHANGE BASED ON CAMERA TRANSFORM FORWARD)

            Debug.Log("using clamped assumed pos");
        }
        else
        {
            AssumedPos = self.transform.position;
        }

I hope you can see the goal, basically I am trying to get the assumed pos to lock on the z axis if self has gone beyond the z position of the target, but if the camera rotates the variables are now mixed up and I can no longer do the comparison with .z pos

Also the direction of the camera is not always simple like this, it might be (1,0,1) for example.

I hope my problem is understandable and if someone can help me I would appreciate it! thanks

Your python is showing… :slight_smile:

Heh. I love it.

So any transform, including the camera, has local helpers for .forward, .right and .up

You can use those shortcuts to be in front of the camera, beside it, etc.

You can also flatten those helpers to the Y plane (or project it onto any plane really) and use the result to drive your “local to camera view” results.

For instance, to be 10 units ahead and 1 unit right of the camera:

Vector3 aheadAndRightOfCamera = cameraTransform.position +
                                cameraTransform.forward * 10.f +
                                cameraTransform.right * 1.0f;