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 PaulSZ4 · Sep 05, 2017 at 03:25 PM · cameraunity 2dcamera-movementfollow player

Make the camera follow the players

I have this script from a tutorial on YouTube that makes the camera follow the player. My problem is, I made more than one character, and I want the camera to follow whichever character is selected.

What I did in other scripts: When the player clicks on a character, this character is set active, instantiated in the scene and the other characters are destroyed.

I have thought about making a dynamic array for the characters but couldn't think of an proper way to do it.

This is the script I'm currently using:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public PlayerController thePlayer;
 
     private Vector3 lastPlayerPosition;
     private float distanceToMove;
 
     void Start () {
         thePlayer = FindObjectOfType<PlayerController> ();
             lastPlayerPosition = thePlayer.transform.position;
     }
 
     void Update () {
             distanceToMove = thePlayer.transform.position.x - lastPlayerPosition.x;
             transform.position = new Vector3 (transform.position.x + distanceToMove, transform.position.y, transform.position.z);
             lastPlayerPosition = thePlayer.transform.position;
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by john-essy · Sep 05, 2017 at 03:54 PM

When you destroy your player you need to give your camera a new target. If i understand correctly you only have one camera.

So when you destroy the player, before you make this call you could do something like Camera.main.gameOBject.GetComponent().thePlayer = myNewPlayer.

Or you could create a method inside your camera script like --

 public void SetTarget(Transform tr)
 {
        thePlayer = tr.GetComponent<PlayerController>().transform;
 }

Then on start of your player controller you could then do

 FindObjectOfType<CameraController>().SetTarget(transform);




Comment
Add comment · Show 3 · 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 PaulSZ4 · Sep 05, 2017 at 05:03 PM 0
Share

I'm sorry for being a complete idiot here, but I don't really understand how that will make the camera detect the selected character. Shouldn't I pass the CharacterID to it or something? (CharacterID is the PlayerPrefs I used to store the ID of the selected character)

Also - again, complete beginner here - I got an error: error CS0029: Cannot implicitly convert type UnityEngine.Transform' to PlayerController' when I tried copying the code you provided.

avatar image PaulSZ4 · Sep 05, 2017 at 05:25 PM 0
Share

If this helps in any way, I noticed I get an error saying that the Player Controller script is destroyed (when the game starts) and the Camera Controller is still trying to access it. Is there a way to have the other players destroyed without destroying the Player Controller? Or am I misinterpreting this error?

avatar image john-essy · Sep 05, 2017 at 08:04 PM 0
Share

That is because in update your camera is looking for the player, but when you destroy it, it no longer exists.

What to do is check to see if you have the PlayerController and if you do carry on with your code else return.

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

107 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

Related Questions

Cinemachine glitch 0 Answers

Unity 2D Fighting Game Camera 0 Answers

Help with a 2d camera Controller 1 Answer

Follow camera in 2d game 2 Answers

Keeping UI Element To Game Object When Camera Moves 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