Calculating target velocity on Y and zooming camera

Hi all, I’m new on the forums, and also a proud Unity3D noob. I’ve been using it for 3 weeks now, fun fun fun…

Here’s my problem:

I have an orthographic camera following it’s target along Z (side-scrolling motorcycle, like Moto X Mayem).
The target is a DirtBike, jumps up and down on Y.

The first thing I need to do is get the bike’s current velocity on Y (it’s a rigidbody). Then I wanna do something like this:

if bike velocity.y is true (so if the bike is moving up), then slowly add 0.5 to camera.orthographicSize until it reaches 15 (default is 7).
Then if bike -velocity.y is true (so if the bike is moving down), then slowly decrease camera.orthographicSize by 0.5 until it goes back to default or until the bike stops moving down.

Here’s the code I have so far, it works ok for the most part, but the zooming effect isn’t as smooth as I would like it to be when it switches from bike going up to bike going down:

var minCamOrthoSize: float	=	7.0;
var maxCamOrthoSize: float	= 15.0;
var smoothTime: float			= 3.5;

function Update ()
{
	var vertBikeVelocity 	= target.rigidbody.velocity.y;
	var currentOrthoSize 	= camera.orthographicSize;
	
	var smoothZoomOut 	= Mathf.Lerp(currentOrthoSize, maxCamOrthoSize, Time.deltaTime * smoothTime);
	var smoothZoomIn 		= Mathf.Lerp(currentOrthoSize, minCamOrthoSize, Time.deltaTime * smoothTime);

	if(vertBikeVelocity > target.position.y)
	{
		camera.orthographicSize = smoothZoomOut;
	}
	else
	{
		camera.orthographicSize = smoothZoomIn;
	}
}

I hope I was clear enough, and thx in advance for any help!!!

Comparing a velocity to a position doesn’t seem to make much sense… :wink: You probably want to change that to

if(vertBikeVelocity > 0.0)

If that doesn’t fix it, post again with more detail about what you’re trying to achieve and/or what’s wrong with what you have.

Hi Laurie, thanks for your suggestion.

The problem with comparing the vertBikeVelocity to 0.0 is that this won’t make the camera zoom back in as the bike is coming down. I need the camera to zoom out as the bike is going up, and then zoom back in as the bike is going down (the bike never goes below 0.0)

What I really need to check for is:

if velocity.y is increasing, then zoom out, else if velocity.y is decreasing, then zoom in.

Any idea on how to script this?
Thanks again!

You can always check the velocity against itself, considering that you’re increasing it via update. I’m not exactly sure if you can do this, but in theory you should be able to check the bike’s velocity before (bikeVel) to its velocity after (bikeVel + deltaTime), and create an if statement out of that.

The bike’s position may not go below zero, but velocity represents change in position over time, and is directed. If the bike is moving up, velocity.y > 0; if the bike is moving down, velocity.y < 0.

Ah I get it…thanks for clearing this up! Works like a champ for doing what I want relative to the up velocity!

Now maybe you can help as well with my next problem?..I need to do the same thing on the Z axis, BUT I need to know when the bike is slowing down so that I can start zooming in.

Here’s what I’m looking for:

If the bike’s forward velocity is increasing, zoom out, and as soon as the forward velocity starts to decrease from it’s current value, start zooming in.

In this case, your solution for the Y velocity doesn’t work since the bike never goes backwards. No matter what, my forward velocity will always be positive, so I need to figure out when it starts to decrease, rather then when it goes below 0.

Thanks a lot for your help again!!!

For that you’ll need to calculate a velocity delta each frame, so you’ll need to store the current velocity every frame to compare against. Something like:

if (lastVelocity <= velocity.x) {
    // zoo out
} else {
    // zoom in
} 
lastVelocity = velocity.x;

Laurie, thanks for your help again.
I’m a little confused as to how I go about storing the velocity delta every frame…do I use:

function Update()
{
lastVelocity = transform.rigidbody.velocity;

or

lastVelocity = transform.rigidbody.velocity * Time.deltaTime;
}

Also, won’t “lastVelocity” always be the same as “velocity.x” (which I suppose is transform.rigidbody.velocity.x right?)

I’m sorry if my questions are stupid, I’m kinda new to programing with only some css and HTML background, so there are many things I still don’t understand or get…thanks for your patience :slight_smile:

Use lastVelocity = transform.rigidbody.velocity.x; if you multiply by Time.deltaTime you wont have a stable base for comparison.

Got it, thanks again!