Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Korpers · Mar 20, 2013 at 06:01 PM · cameratracking

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

Comment
Add comment · Show 2
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 robertbu · Mar 20, 2013 at 06:03 PM 0
Share

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.

avatar image Korpers · Mar 20, 2013 at 06:21 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 5 · 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 · Mar 20, 2013 at 08:09 PM 0
Share

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?

avatar image robertbu · Mar 20, 2013 at 08:40 PM 0
Share

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.

avatar image Korpers · Mar 20, 2013 at 11:58 PM 0
Share

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?

avatar image robertbu · Mar 21, 2013 at 03:07 AM 0
Share

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().

avatar image Korpers · Mar 21, 2013 at 09:31 AM 0
Share

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.

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Main Camera child under player, causes flip 3 Answers

How can I accurately convert a Game Object's position to a sub Canvas/Rect screen position? 0 Answers

Extended Tracking losing target issues 0 Answers

Where can i enable TrackingBehaviour on scene load? 0 Answers

Object/Landmark Tracking using mobilephone 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