fixed Point at the camera.

hello, i’ve made an isometric camera, and when my player moves, the camera follows him. how can i change the distance between the player and the camera, and keep the player at the center of the camera as usual?

cam code:

public var player:GameObject;
//The offset of the camera to centrate the player in the X axis
public var offsetX = -2;
//The offset of the camera to centrate the player in the Z axis
public var offsetZ = -20;
//The maximum distance permited to the camera to be far from the player, its used to     make a smooth movement
    public var maximumDistance = 1;
//The velocity of your player, used to determine que speed of the camera
public var playerVelocity = 20;

    private var  movementX;
private var movementZ;




// Update is called once per frame
    function Update ()
    {
	
        movementX = (player.transform.position.x + offsetX - this.transform.position.x)/maximumDistance;
        movementZ = (player.transform.position.z + offsetZ - this.transform.position.z)/maximumDistance;
		if (maximumDistance >=1)
		{
        this.transform.position += new Vector3((movementX * playerVelocity * Time.deltaTime), 0, (movementZ * playerVelocity * Time.deltaTime));
		}
		else
		{
		maximumDistance = 1;
		this.transform.position += new Vector3((movementX * playerVelocity * Time.deltaTime), 0, (movementZ * playerVelocity * Time.deltaTime));
		}
		
    }

thank you :stuck_out_tongue:

Hi, welcome to the forum!

Rather than keep the camera at a fixed XZ offset from the player, you can set a vector at the correct angles for isometric projection and then add a multiple of this vector to the player’s position to get the correct camera position. An easy way to think of this is that the vector for the camera angle should point straight from one corner of a cube to the exact opposite corner (since these corners should line up perfectly under isometric projection). This is the <1,1,1> vector which is conveniently available as Vector3.one. To get an exact distance from the player to the camera, you should normalise this vector. Since its length is Sqrt(3) or 1.732, you can do this as follows:-

var directionVector = Vector3.one / 1.732;

Then, you just need to multiply this vector by the desired distance from camera and add it to the player’s position to get the correct camera position. The direction the camera should look is just the negation of the direction vector and you should only need to set this once:-

camera.transform.position = player.transform.position + directionVector * camDistance;

If you have the game action arranged so that the camera looks from the other Z or X direction (or both) then instead of using Vector3.one, you should just use

var directionVector = Vector3(-1, 1, -1) / 1.732;

…etc. The code is otherwise exactly the same.

thankyou