Moving second gameObject based on Lerpz moves?

Hi guys, man I’m on day 4 of this fun problem I ran into. I searched the forums and can’t find a solution, wonder if you guys can help me please? I’m on the Lerpz 3D platformer tutorial btw. I’m trying to have a second gameObject (a cube) follow above Lerpz but only on the X axis. My reason is I’m trying to have Lerpz move freely along a rectangular level but the mainCamera is attached to the second gameobject (cube) so that the player’s view goes along a track on the X axis. Lerpz moves freely but the mainCamera moves along the X axis only. Is this possible to do? Any help would be greatly appreciated, thanks again.

You could store the Y and Z positions of the camera rail and then match the X position to the character:-

var railMode: boolean;
var railY: float;
var railZ: float;
var player: Transform;


function Update() {
    if (railMode) {
        transform.position = new Vector3(player.position.x, railY, railZ));
    } else {
        // normal camera
    }
}

Hi Andeeee, thanks for the reply. I added the script to the cube gameObject above Lerpz and it works!!! The camera follows the cube above Lerpz and moves only on the X axis. I just have to find a way to smooth the camera movement though, it’s quite jerky right now.

Thanks again.

UPDATE: Hey Andeeee, got any idea why when I move Lerpz around the rectangle level, the camera follows smoothly until I press 2 arrow keys (for example: up and right arrow) then the whole camera shakes the game screen?

Thanks again. Very much appreciated! :smile:

Does this happen while the camera is following the rail or when it is moving freely?

Hi andeeee, sorry I’m answering late. The jerkiness happens when I change Lerpz movement. Like, for example, level loads and I press the up arrow: Lerpz moves okay and the camera follows cleanly but as soon as I press another arrow key, the screen jerks and then smooths out until I change the direction Lerpz is moving. Basically, any change in the direction Lerpz moves causes the ‘jerky’ camera.

Thanks again.