Somethings not right with this 3d camera script

I’m pretty new to unity but iv done some coding in the past. I have got some code for an orbital camera that sets to my target. The camera works…but… Only if I have the right mouse button down. For some reason its not updating and staying following the target after I let go. I’m probably missing something extremely basic here but if someone could give me a point in the right direction that would be awesome :slight_smile: I just doesnt do anything unless I have the right mouse button press down: it doesnt even scroll unless I have the mouse button pressed.

public Transform playertarget;

    private float x = 0.0f;
    private float y = 0.0f;

    private int mouseXSpeedMod = 5;
    private int mouseYSpeedMod = 3;

    public float maxVeiwDistance = 25; // how far the camera zooms out
    public float minVeiwDistance = 3; // How close the camera zooms in
    private float distance = 3; // Starting distance from player
    private float desiredDistance; // used for calculations
    private float correctDistance; // used for calculations
    public int zoomRate = 30; // How fast the camera will zoom

    public int lerpRate = 5;
    public float playerTargetHight = 1.0f;
    private float currentDistance;






    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.x;
        y = angles.y;

        currentDistance = distance;
        desiredDistance = distance;
        correctDistance = distance;




    }

    // Update is called once per frame
    void Update()
    {

    }

    void LateUpdate() //Update camera every frame
    {





        if (Input.GetMouseButton(1))
        {
            x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
            y += Input.GetAxis("Mouse Y") * mouseYSpeedMod;
        }

        else if (Input.GetAxis("vertical") != 0 || Input.GetAxis("Horizontal") != 0)
        {
            float targetRotationAngle = playertarget.eulerAngles.y;
            float cameraRotationAngle = transform.eulerAngles.y;

            x = Mathf.LerpAngle(cameraRotationAngle, targetRotationAngle, lerpRate * Time.deltaTime);
        }


        y = ClampAngle(y, -50, 80);

        Quaternion rotation = Quaternion.Euler(y, x, 0);

        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); // calcualtes the distance between teh camera and the player
        desiredDistance = Mathf.Clamp(desiredDistance, minVeiwDistance, maxVeiwDistance);
        correctDistance = desiredDistance;

        Vector3 position = playertarget.position - (rotation * Vector3.forward * desiredDistance);

        RaycastHit collisionHit;

        Vector3 playerTargetPosition = new Vector3(playertarget.position.x, playertarget.position.y + playerTargetHight, playertarget.position.z);

        bool isCorrected = false;
        if (Physics.Linecast(playerTargetPosition, position, out collisionHit))
        {
            position = collisionHit.point;
            correctDistance = Vector3.Distance(playerTargetPosition, position);
            isCorrected = true;
        }

        currentDistance = !isCorrected || correctDistance > currentDistance ? Mathf.Lerp(currentDistance, correctDistance, Time.deltaTime * zoomRate) : correctDistance;

        position = playertarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -playerTargetHight, 0));




        transform.rotation = rotation; // when you call transform within the script, it looks for the transform the script is atacthed too.
        transform.position = position;

    }
    private static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
        {
            angle -= 360;
        }
        if (angle > 360)
        {
            angle += 360;
        }
        return Mathf.Clamp(angle, min, max);
    }

Look into Execute Order, FixedUpdate should be where all your Physics logic should be as it gets called a fixed number of times vs Update where it is variable. Knowing this, if you want to follow a target you need to update the following object every frame since you don’t know where the target will be. Any changes will be updated since you are checking every frame. From what I can see, you are not doing that. Then again its hard to read your code since you are not using code tags. Please update your post so your code can be legible.

Code tags have been put on sorry it was late :stuck_out_tongue: Would I have to have bool values in fixed update?

so only when you right clicking your x und y are changing.

your elsif,

just check with a Debug.Log if he is even going in here and what resuklts you geht.

use some debug logs at all to check which values your variables have. or breakpoints if you can.

Got it working :slight_smile:

1 Like

can you tell us how you fixed it?

It was actually just a simple typing error on my part.else if (Input.GetAxis("vertical") != 0 || Input.GetAxis("Horizontal") != 0) I have to change the lower case “v” in vertical to upper case :slight_smile: