Matching upwards velocity of 2 objects

I have 2 objects one is a player object and the other is an invisible box Colider parented to the camera. now when the player comes too close to the plane the camera moves up at the same speed as the player which works great with a player that moves like the spaceship in R-Type with a fixed movement speed but i now need to apply this to a character that moves more like mario so when the the player jumps the camera is very jumpy because its moving faster than the player most of the time.

this is the code that i wrote for the original purposes

var LeftPane : Transform;
var LeftPaneContactSpeed = 5;
var RightPane : Transform;
var RightPaneContactSpeed = 60;
var TopPane : Transform;
var TopPaneContactSpeed = 120;
var BottomPane : Transform;
var BottomPaneContactSpeed = 120;
var Player : Transform;
var Constant = true;
var ConstantSpeed = 15;
public var DistanceLeft : float;
public var DistanceRight : float;
public var DistanceTop : float;
public var DistanceBottom : float;
public var MinimalDistanceLeft : float;
public var MinimalDistanceRight : float;
public var MinimalDistanceTop : float;
public var MinimalDistanceBottom : float;

function Update () 
{

	DistanceRight = Player.renderer.bounds.min.x - RightPane.position.x;
	DistanceLeft = Player.renderer.bounds.min.x - LeftPane.position.x;
	DistanceTop = Player.renderer.bounds.min.y - TopPane.position.y;
	DistanceBottom = Player.renderer.bounds.min.y - BottomPane.position.y;



		if (DistanceRight < MinimalDistanceRight)
		{
			transform.Translate(RightPaneContactSpeed*Time.deltaTime, 0, 0);
		}
			else
			{
				if (DistanceLeft > MinimalDistanceLeft)
				{
					transform.Translate(LeftPaneContactSpeed*Time.deltaTime, 0, 0);
				}
		
				else
				{
					if (Constant == true)
						{
							transform.Translate(ConstantSpeed*Time.deltaTime, 0, 0);
						}
				}
			}	
			
			
			if (DistanceTop > MinimalDistanceTop)
		{
			transform.Translate(0, TopPaneContactSpeed*Time.deltaTime, 0);
		}
			else
			{
				if (DistanceBottom < MinimalDistanceBottom)
				{
					transform.Translate(0, -BottomPaneContactSpeed*Time.deltaTime, 0);
				}
			}	
			
				
}

what i need to do is track the upward movement velocity of the player and setting that value to

var TopPaneContactSpeed = 120;

but i’m clueless as to how to obtain the upwards velocity of the player.

can anybody help me here?

This may be overly-simplified, but how about just matching the camera’s Y position with the player’s Y position?

/*
fl_offset_y is the minimum vertical difference between the camera and the player
go_player is the player's game object
go_camera is the camera
*/

function Update() {
    if (go_player.transform.position.y > go_camera.transform.position.y + fl_offset_y) {
        go_camera.transform.position.y = go_player.transform.position.y - fl_offset_y;
    }
}

I tried this method and it works but it causes a jarring jumpy movement. the only solution i can think of would be to lower the timestep (Not Gonna Happen).

i want to try my original idea of obtaining the y velocity of of the player and setting it to a variable which can be used to transform the position of the camera

with something like

transform.Translate(0, velocity_y*Time.deltaTime, 0);

but i dont know how to get the velocity of the player and apply it to a variable like.

var velocity_y = Player.velocity.y;

or some such

Edit: I have it solved thanks to the example above from BrettFromLA i changed “function Update” to “function LateUpdate” and it fixed the jaring movement issue.

Thank you :twisted:

Glad that worked! I’ve never used function LateUpdate() – now I know what it’s good for!