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 Steyo · Aug 16, 2020 at 10:19 AM · vector3beginnercamera followgolf

How can I get my camera behind my moving ball ?

I'm trying to do something simple. In a golf game, I want my camera to follow the ball at a certain distance. So basically I created a Vector 3 offset and in my camera update I set my camera to the position of the ball + my offset.

 Vector3 offset;
 offset = new Vector3 (-3, 1, -1.5f);
 
 //update
 Vector3 pos = ball.transform.position + offset;
 transform.position = pos;

However, the camera offset seems to change depending of the shot : alt text

Here the ball is on the right of thecamera and on the next shot: alt text

the ball is behind the camera.

shot-1.jpg (132.8 kB)
devant.jpg (45.4 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ShySam · Aug 16, 2020 at 10:51 AM

@Steyo you can try to increase the offset or use (int) values incited of numbers in the offset like:-

//put these values out of any void .. no need to bu them under the void update to use them public int Value1 = -3 public int Value2= 1 public int Value3 = -1.5

//then add them to the offset like this: offset = new Vector3 (Value1 , Value2, Value3 ); . also you can make the camera look at the ball at all times by adding this line to void ubdate [ transform.forward = lookToward.normalized; ] (don't take the brackets [] ! . if that did't work look fore (Third person camera follow script )

Comment
Add comment · Show 1 · 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 ShySam · Aug 16, 2020 at 10:54 AM 0
Share

@Steyo the secreipt will be like this\\

public int Value1 = -3 public int Value2= 1 public int Value3 = -1.5

Vector3 offset; offset = new Vector3 (Value1 , Value2, Value3 );

void update () { Vector3 pos = ball.transform.position + offset; transform.position = pos; }

avatar image
0

Answer by highpockets · Aug 16, 2020 at 11:45 AM

The offset you are giving is not changing based on the ball’s moving direction, assuming the ball has a parent transform that doesn’t rotate with the ball as it rolls, you can just make the camera a child of that and it should take care of this for you, but otherwise you need to get the offset based on the balls movement direction.


 //You could get the movement direction from the rigid body velocity for example, but let’s assume you have that direction and it’s normalized for brevity sake
 
 Vector3 localPos = -ballMoveDirection * 1.5; //a local position 1.5 units behind the ball
 
 //add some height to That position
 localPos = new Vector3(localPos.x, localPos.y + 3, localPos.z);
 
 //place the cam at the new pos
 camTransform.position = ballTransformPosition + localPos;
 
 //now make sure the cam is looking at the ball
 camTransform.LookAt(ballTransform);
 
 

You might have to customize it to fit your needs, but this is more or less what you need

Comment
Add comment · Show 2 · 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 Steyo · Aug 16, 2020 at 12:02 PM 0
Share

This look clear enough but how do I get ball$$anonymous$$oveDirection ? It's a Vector3 I'm supposed to get from Rigidbody ?

avatar image highpockets Steyo · Aug 16, 2020 at 03:55 PM 0
Share

You can get the move direction if the ball has a rigidbody by accessing the velocity vector and normalizing it. Otherwise just test the last frame position vs current frame position of the ball and the current position $$anonymous$$us the last position is the direction, but then remember to normalize it to be able to use it effectively.

One thing to think about is if the ball moves upwards, this will make the camera go downwards, so you might want to flatten the directions over a constant plane so the camera is always above the ball. For that, you can you Vector3.ProjectOnPlane which takes the normal of the plane and the direction you want to flatten onto it as parameters and returns the flattened direction.

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

181 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Controller to Player Position Conflict 1 Answer

Wht isn't the sphere moving and rebounding properly? 1 Answer

Enemy spawn script/player position(java/js) 1 Answer

Trying to create a camera that follows player and mimics headset tracking(HTC Vive) 1 Answer

Move one object with another, without parenting it to the object? 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