Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 BillyGD · Apr 12, 2018 at 06:14 AM · camera movement

Setting the camera position depending on the position of 2 objects

Hi guys, I've been working on this for 3 days now and I can't for the life of me figure it out!

My game is a 3D turn based football game where you drag the players back to add power and let go to 'catapult' them towards the direction you are aiming in. The game can be found here https://gamejolt.com/games/flickfootball3d/326078

Basically when I right click a player, I want the camera to go behind the player, facing the ball object like below (the player circled is the one that has been clicked): alt text

I want the camera to always be a set distance away from the player no matter what the position of the ball is, but always facing towards the ball.

The best way I can describe it is that I want a player quick select where you can jump from player to player easily to line a shot up.

I would be very grateful if someone could point me in the right direction as this is driving me insane! I hope I've managed to describe my problem in a clear way.

Thanks, Billy.

unity-question-01.jpg (134.7 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
2
Best Answer

Answer by BastianUrbach · Apr 12, 2018 at 08:11 AM

You take the direction from the player to the ball along the ground, multiply it by the distance you want the camera to be away from the player, subtract that from the player position and move it up a little. Finally, you just make the camera look at the ball.

Vector3 dir = (ball.position - player.position);
dir = new Vector3(dir.x, 0, dir.z).normalized;
camera.position = player.position - dir * distance + Vector3.up * height;
camera.LookAt(ball, Vector3.up);
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 BillyGD · Apr 12, 2018 at 11:15 PM 0
Share

Thank you so much it worked perfectly! I was so close yet so far with everything I tried! Really appreciate your help

avatar image
1

Answer by tormentoarmagedoom · Apr 12, 2018 at 08:10 AM

Good day.

You need to create a script to do this, I will not write the code correctly because i'm not used to use this type of functions, you must look for the correct name of functions and how write them, but the idea must be this:


You have 3 elements, Ball, Player, Camera.

First, using always the ball as reference, you need to calculate the direction from the ball to the selected player (ball.position-player.position).


Then calculate the distance (using Vector3.Distance) between ball and the player.


Once you know the direction and the distance, you should be able to move the camera to the same direction and (for example) 2 times the distance player-ball.

So now will have always a "imaginary line ball-player-camera".

And dont forget to make the camera look at the ball with "LookAt" function.

The concept of your script sholud be thisone. Now, It's your turn to find the functions to do it!


If helped, accept the answer and close the question!

Bye!

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 BillyGD · Apr 12, 2018 at 11:18 PM 1
Share

Thank you so much for your reply, really appreciate it!

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

77 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

Related Questions

[C#] Smooth Camera Movement Restrictions 0 Answers

Creating a file that records the player position during a game in xyz coordinates 0 Answers

How to move and rotate character according to camera? 0 Answers

HOW TO VIBRATE CAMERA ACCORDING TO AUDIO BEATS ? 0 Answers

2D game. How do I make the camera follow the player until it reaches the edge of a section of the map? 0 Answers


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