Calculate Distance from target

Hi all.
I have the following setup.
Whenever the player moves I calculate the x and z distance from the camera and if > xDistance/zDistance Lerp to the center.
The problem is when I rotate the camera with the right stick (no code below) with the player still in view the distances because of the change in rotation are off. The camera will still be on the player but the xDistance is no longer starting at zero from the center.
No problem when not rotating the camera because the distances are then linear.
Thanks for any assist.

 Vector3 follow = new Vector3(followXForm.position.x, followXForm.position.y, followXForm.position.z);
        Vector3 offset = new Vector3(0, yDistance, -zDistance);
        //Vector3 target = follow + rotationAdd * offset;
        Vector3 target = follow + offset;

        float xD = Mathf.Abs(transform.position.x - target.x);
        float zD = Mathf.Abs(transform.position.z - target.z);
        //xD = 1000f;
        //float smooth = (Mathf.Log(xD) * smooth2D); // lower the smooth number faster the cam follows


        if (xD >= xOffset && Math.Abs(inputController.leftStickX) > 0)
        {
            Debug.Log("xD " + xD);

            float smooth = 0;
            if (xD > 0) smooth = smoothMovement / Mathf.Log(xD); // lower the smooth number faster the cam follows
            //float smooth = smooth2D; // lower the smooth number faster the cam follows
            Vector3 ps = new Vector3(target.x, target.y, target.z);
            transform.position = Vector3.Slerp(transform.position, ps, Time.deltaTime * smooth);
            //transform.position = ps;
            //transform.forward = transform.TransformDirection(Vector3.forward);//forward of camera to forward of world


        }

        if (zD >= zOffset && Math.Abs(inputController.leftStickY) > 0)
        {
            float smooth = 0;
            if (zD > 0) smooth = smoothMovement / Mathf.Log(zD); // lower the smooth number faster the cam follows
            //float smooth = smooth2D; // lower the smooth number faster the cam follows
            Vector3 ps = new Vector3(target.x, target.y, target.z);
            transform.position = Vector3.Slerp(transform.position, ps, Time.deltaTime * smooth);
            //transform.position = ps;
        }

What are you trying to accomplish? There’s probably an easier way to go about it but I can’t know what that is as I don’t know what final result you are after.

Are you just trying to get the camera to follow the player at a set distance?

Thanks but sorry if its confusing :(.
Rather than have the camera look at the player I create an offset. Old school Mario style cam.
the camera is on the player. if the player moves x to the left or right the camera will not move. Once the player is more than X away the camera will now lerp to the player with the result being the camera is now directly behind the player again. (0 offset).

ie
x distance 5
camera (0,10,-10)
player starts (0,0,0)
player moves to (5,0,0) - camera remains still
player moves to (10,0,0) - camera lerps to ((10,0,-10) because the x difference is now more than 5

Now my issue is I am using the right stick to allow rotation of the camera.
I believe its basic local / world coordinate issues but I just can’t get a grip on it.
If the camera is rotate for example where it is now directly to the left of the player ie (-10.0,0)
even though the camera rotates to still face directly at the player the x Difference is now calculated as 10 because the camera is
(-10,0,0) in the world and the player is (0,0,0) where xDistance = abs(-10-0)
I need oit calculated as zero.

I’m pretty sure its simple but only if I can explain it properly :slight_smile:

Thanks!

Using InverseTransformPoint seems to work ??

Vector3 follow_local = followXForm.InverseTransformPoint (transform.position);
        Vector3 cam_local = transform.InverseTransformPoint (followXForm.position);
        float xD = Mathf.Abs(cam_local.x - follow_local.x);
        float zD = Mathf.Abs (cam_local.z - follow_local.z);
1 Like