• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Korpers · Apr 04, 2013 at 10:53 PM · zoomcamera movementtweentweening

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.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Korpers · Apr 05, 2013 at 08:07 AM 0
Share

Guess it's impossible then huh?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
Fattie

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Korpers · Apr 05, 2013 at 06:48 PM 0
Share

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.

avatar image iwaldrop · Apr 05, 2013 at 06:58 PM 0
Share

Sure! Just post another comment if you run into problems.

avatar image Korpers · Apr 07, 2013 at 11:31 AM 0
Share

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 minDist = 7.0;
 var maxDist = 1000.0;
 var speed = 25.0;
 
 float speedModifier = 1 + (ballPos.position - transform.position).sqrMagnitude, (desiredDist * desiredDist);
  
 function Update () 
 {
     var f = Mathf.MoveTowards(myCamera.position.y - target.position.y, desiredDist, speed * speedModifier * 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 = Mathf.Clamp(f, minDist, 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 speedModifier = 1 + (ballPos.position - transform.position).sqrMagnitude, (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.

avatar image iwaldrop · Apr 07, 2013 at 05:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dotween: Multiple .OnComplete don't work? 1 Answer

GameObject to follow another GameObject through a spline with a dynamic end waypoint 0 Answers

I get ": The private field 'nameofmyvariable' is assigned but its value is never used" thanks to DOTWEEN 1 Answer

leantween ltspline control points setup 0 Answers

Implement zoom in standard assets free look camera 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges