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
1
Question by tool55 · Nov 16, 2010 at 02:39 PM · distanceswitchcamerassortautomatically

switch between multiple cameras based on distance to target

Hi,

I'd like to have multiple cameras placed around a map and have the cameras switch automatically depending on which is closest to the player so that they follow the players movements no matter where he goes.

I've attached a basic LookAt script to the cameras, but the problem is the switching. How would I determine which camera is closest to the player and enable it, then disable all others.

I figured that putting the cameras in an array (somewhat like waypoints) might work, but I'm having trouble figuring out how to determine which of the cameras in the array is closest to the player. I understand Vector3.Distance. The problem is, how do I iterate through the array and pick the one with the shortest distance? Thanks for the help.

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
1

Answer by Jesse Anders · Nov 16, 2010 at 02:58 PM

You have the right idea with putting the cameras in an array and iterating over them. As for the problem of finding the camera closest to the player, here's some pseudocode (untested):

// Find the closest point in an array of points to a query point 'p':
float minDistance = float.MaxValue;
int closest;
for (int i = 0; i < points.Length; ++i) {
    float distance = Vector3.Distance(p, points[i]);
    if (distance < minDistance) {
        minDistance = distance;
        closest = i;
    }
}
// 'closest' is now the index of the closest point to 'p'.

Adapting this to find the camera closest to the player should be straightforward. (Note that this can be optimized slightly by comparing squared distances rather than distances. However, any difference in performance is unlikely to be noticeable in this context.)

Comment
Add comment · 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
1

Answer by ptulipan · Nov 18, 2010 at 01:56 AM

Thanks, Jesse. You're posts are always very helpful. It will take me a couple of days to adapt this to my purposes. Once I do, I'll post it here for others to use. You're the best.

Here's the code based on your example. It's in Javascript.

var multicam : Camera[]; static var myrocket: GameObject; myrocket = GameObject.FindWithTag ("rocket");

function Update () { var minDistance: float = 5000; var closest: int;

for (i = 0; i < multicam.Length; i++) { var distance = Vector3.Distance(myrocket.transform.position, multicam[i].transform.position); if (distance < minDistance) { minDistance = distance; closest = i; Debug.Log(closest); } for (cameras in multicam) { if (cameras !=multicam [closest]) { cameras.enabled = false; cameras.GetComponent(AudioListener).enabled = false; } } multicam[closest].enabled = true; multicam[closest].GetComponent(AudioListener).enabled = true; } }

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Sort By Distance, Call it multiple times. 2 Answers

Multiple Camera Switching 1 Answer

Trouble switching cameras 1 Answer

Cannot switch cameras!!! Help ASAP!!! 1 Answer

Resize Array based on distance. 2 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