- Home /
Cannot tween camera to a nice smooth stop.
Hi,
I am currently using a camera to track a golf ball in the air. The camera tracks the ball from above and then when gravity gets the better of the ball, it drops away from the camera leaving a view in the sky of the ball on the fairway below. My camera then begins to zoom back to the (close-up) distance to the ball as specified in the script, zooming quickly (as i have specified).
How can I tween the camera to a stop when it reaches the ball again? Looks pretty crude at the minute.
Here is the script I am using:
var myCamera : Transform;
var target : Transform;
var desiredDist = 7;
var minDist = 7;
var maxDist = 1000;
var speed = 40;
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);
}
At the minute the camera just crashes to a stop with this method. There is also a little 'judder' when the ball falls past the desired distance too, that I'm trying to smooth out, but as I'm quite new to JS I just can't figure it out. Thanks for any help.
Answer by iwaldrop · Apr 05, 2013 at 06:32 PM
It's not impossible. Part of the problem might be the use of clamp. That's going to override the smoothing that occurs in movetowards. What you probably want to do is multiply your speed by a function of the camera's distance from the ball. That means that the further the ball is away from the camera, the more you're going to add to a multiplier. You could do something like the following, although it's probably not going to work as is for your usecase, but with some fiddling it will.
float speedModifier = 1 + (ballPos.position - transform.position).sqrMagnitude - (desiredDist * desiredDist);
Then you'd also multiply your speed value by the above value.
var f = Mathf.MoveTowards(myCamera.position.y - target.position.y, desiredDist, speed * speedModifier * Time.deltaTime);
Let me know how it worked for you.
Thanks for your help. I will try it first thing in the morning and let you know! I'm bit of a noob so bear with me.
Sure! Just post another comment if you run into problems.
Hi again. Ok - I'll be totally honest. I am learning to script as best I can, but just when I think I understand, something comes along and is just way too advanced for me. The script I posted was something I was shown previously, and I modified (very slightly) to get to work.
I have taken the code you have laid out above and tried my best to get it working - but I just think I'm not advanced enough to know what to do correctly. Let me show you what ive done:
var myCamera : Transform;
var target : Transform;
var desiredDist = 7.0;
var $$anonymous$$Dist = 7.0;
var maxDist = 1000.0;
var speed = 25.0;
float speed$$anonymous$$odifier = 1 + (ballPos.position - transform.position).sqr$$anonymous$$agnitude, (desiredDist * desiredDist);
function Update ()
{
var f = $$anonymous$$athf.$$anonymous$$oveTowards(myCamera.position.y - target.position.y, desiredDist, speed * speed$$anonymous$$odifier * Time.deltaTime);
myCamera.position = target.position + Vector3(0, f, 0);
}
Firstly I put the 'float' speedmodifier in the active script section as I thought this was the correct place to put it. Looking at it now, i think perhaps I should put it back in the update section, as this figure needs to be updated every frame doesn’t it? ballPos obviously refers to something but I don’t know how to set it up.
I then put the new var f
where I had the old similar code. I took out the f = $$anonymous$$athf.Clamp(f, $$anonymous$$Dist, maxDist);
bit as I remember you saying this was the reason it was not tweening – and then left the myCamera.position = target.position + Vector3(0, f, 0);
line as that looked like it should still work.
The error I’m getting is that a semicolon is expected at the end of float speed$$anonymous$$odifier = 1 + (ballPos.position - transform.position).sqr$$anonymous$$agnitude, (desiredDist * desiredDist)
despite there being one there.
I have tried starting from scratch with this, so I didn’t have to come back here and look like a total n00b, I’ve even looked on the asset store for an ‘all in one’camera script, but they all seem suited for isometric shooters and not really for my needs.
So here I am again! Where on earth do I start with this? Sorry for my complete lack of skill in this area.
Your first thought is correct, it should be figured every frame. It shouldn't (and seems like it doesn't) even compile with a line like that outside of a method body.
Are you familiar with math? If you were, you'd see that this is a very basic equation which gets the square magnitude distance from the ball to the camera, and then subtracts the desired distance squared from it. It's a simple distance calculation. I'm not sure which needs to be subtracted from the other for your camera to follow properly; you'll have to play with that.
Also, there are no commas in the code I posted. It's a bunch of addition and subtraction, there are no method arguments.
Try the code that I posted in your update loop, and just multiply speed by the var it creates. And, since you're using JavaScript, replace float win var.
Your answer
Follow this Question
Related Questions
loop a chain of iTween actions 0 Answers
LeanTween, Problem with slower device when loop create and cancel tween in sequences 0 Answers
Implement zoom in standard assets free look camera 1 Answer
DOTween, tk2dTextMesh and float 0 Answers
LeanTween, Tween canceled but still true when check isTweening. 0 Answers