- Home /
Camera Tracking with a little Y freedom from the game object.
Hi,
I am trying to track a ball from above (golf game) with my camera. I have easily done this with parenting the ball to the camera so the camera is a child - but I'd like the camera to have a little flexibility on the Y axis, so I can see the ball grow and shrink as it goes higher and lower.
My other idea is to change the size of the ball object whilst at various heights, but again - im not sure how to do this smoothly.
Has anybody had experience with this at all?
Thanks for reading.
Korpers
It is difficult to offer specifics without your code. I'd do a combination of Vector3.Lerp to create a bit of lag in the y movement along with creating a $$anonymous$$imum distance to the ball.
Ooops, yes that would of been a good idea wouldn't it. Here is the super basic code:
#pragma strict
var myCamera : Transform;
var target : Transform;
function Update ()
{
myCamera.position = target.position + Vector3(0, 20, 0);
}
I'll try putting Lerp into the Vector3 class (is it a class?) now. Thanks for answering.
Answer by robertbu · Mar 20, 2013 at 06:54 PM
Here is a bit of untested code to show you what I was talking about. You'll have to play with the parameters in the Inspector to get something that feels right or your situation.
var myCamera : Transform;
var target : Transform;
var desiredDist = 20.0;
var minDist = 5.0;
var maxDist = 25.0;
var speed = 30.0;
function Update ()
{
var f = Mathf.MoveTowards(myCamera.position.y - target.position.y, desiredDist, speed * Time.deltaTime);
f = Mathf.Clamp(f, minDist, maxDist);
myCamera.position = target.position + Vector3(0, f, 0);
}
Hi robertu,
Thanks for this! I tried it and messed about with the values, and it certainly does what I asked for. The only issue I can see is that when it comes close to the camera on the way up it really chuggs the camera upwards and looks like it is jittering on the spot until it starts to go back down again.
I'm thinking it might actually be a better idea to change the size of the golf ball object depending on where it is in the world 3d space - considering that im trying to make a 2D game and I'm currently using a perspective a camera, which really, I should flick to perspective.
What do you think?
Try the size and see. It's worth a shot if it gives you what you want. Personally I just keep working on the code above until it feels right. Change the $$anonymous$$athf.$$anonymous$$oveTowards() to $$anonymous$$athf.Lerp() and see if it works any better. You'll have to change speed values as well. You could also dynamically change the values. For example, you could increase the desiredDist just as the ball is hit to cause the camera to pull away and then restore it when the ball hits the ground.
Thats a great idea ("you could increase the desiredDist just as the ball is hit to cause the camera to pull away and then restore it when the ball hits the ground.") but how on earth do i do this?
There are a number of ways of doing it. What you do will depend on how you'e setup your game. The starting point is accessing one game object from another. See Accessing Other Game Objects. You will also need to detect when the ball collides with the the ground again. See OnCollisionEnter().
Thanks I will try all your suggestions. I'm very new to both Unity and Javascript, so no doubt I will be back soon with more conundrums. Thanks again.