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
1
Question by TheAMD · Apr 25, 2016 at 05:59 PM · camerainstantiateplayer

Locking the Camera to Instantiated Player

Hi guys, having an issue getting this to work. I have the player select a ship, which assigns them an Int, using the Int, I initiate into the 'Game' screen their ship through the Gamemanager, this screen doesn't have a Camera, as I want it to be linked to the player, and only focus on them, so the PlayerPrefab has the Camera script, however, it doesn't work, what am I doing wrong, I'm stumped..

Inst'd Code

 using UnityEngine;
 using System.Collections;
 
 public class PlayerInstantiateScript : MonoBehaviour {
 
     //Instatiate based on which bool.
     public GameObject PlayerRocket1;
     public GameObject PlayerRocket2;
     public GameObject PlayerRocket3;
     
     // Use this for initialization
     void Start () {
 
         if (ChangeShipName.ShipChoice==1)
         {
         
             Instantiate(PlayerRocket1, new Vector3(531, 59, 0), Quaternion.Euler(270, 0, 0));
         }
         else 
             if (ChangeShipName.ShipChoice==2)
         {
             
                 Instantiate(PlayerRocket2, new Vector3(531, 59, 0), Quaternion.Euler(270, 0, 0));
         }
         else
             if (ChangeShipName.ShipChoice==3)
         {
             
                 Instantiate(PlayerRocket3, new Vector3(531, 59, 0), Quaternion.Euler(270, 0, 0));
         }
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

Camera Code

 using UnityEngine;
 using System.Collections;
 
 public class CameraStalkPlayer : MonoBehaviour {
 
 
     public Transform player;
     public Vector3 offset;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.position = new Vector3 (player.position.x + offset.x, player.position.y + offset.y, offset.z); // Camera follows the player with specified offset position
     }
 }
 




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 RakshithAnand · Apr 25, 2016 at 07:09 PM 0
Share

What do u mean by screen? By looking at the code u mean to say u want $$anonymous$$ain camera to follow the "instantiated player" as soon as it is instantiated?

avatar image TheAMD · Apr 26, 2016 at 06:42 AM 0
Share

Thats right, centered on it, and flollowing :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by korbul · Apr 26, 2016 at 07:30 AM

It's not exactly clear what you're trying to do from your questions.

However, if you want to have a camera follow a transform that is created at runtime you can do it like this.

Create a camera with CameraStalkPlayer on it in the scene where you will instantiate the player rocket.

Add a public CameraStalkPlayer in your PlayerInstantiateScript and set it to the newly created camera in the editor.

Now you can do

     void Start () {
      
              if (ChangeShipName.ShipChoice==1)
              {
                  GameObject player = Instantiate(PlayerRocket1, new Vector3(531, 59, 0), Quaternion.Euler(270, 0, 0)) as GameObject;
                  cameraStalk.player = player.transform;
              }
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 TheAMD · Apr 26, 2016 at 07:50 AM 0
Share

Just going to try this now, when you say to create the public CSP do you mean a GameObject?

avatar image korbul TheAMD · Apr 26, 2016 at 08:34 AM 0
Share

No, you can just make it Public CameraStalkPlayer; , and when you drag the camera object with a CSP attached, Unity will automatically set a reference to that script.

avatar image
-1

Answer by RakshithAnand · Apr 26, 2016 at 08:38 AM

@TheAMD You need to make your CameraStalkPlayer script's "player" variables public static in order to access it from another script (unless u design it as a singleton - which you can research on it further)

But for now do this:

 public class CameraStalkPlayer : MonoBehaviour {
  
  //......
      public static Transform player;
  //.....
  }

and in the other script like this

 public class PlayerInstantiateScript : MonoBehaviour {
 
 // .....
          if (ChangeShipName.ShipChoice==1)
          {
          
              GameObject playerInstance = Instantiate(PlayerRocket1, new Vector3(531, 59, 0), Quaternion.Euler(270, 0, 0));
              CameraStalkPlayer.player = playerInstance.transform;
          }
 // .....
  }




Comment
Add comment · Show 7 · 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 korbul · Apr 26, 2016 at 10:47 AM 0
Share

What you are proposing here is easier but not the correct way. Static variables should be used when needed. What happens if you have multiple CameraStalkPlayer scripts? you just lost scalability.

avatar image RakshithAnand korbul · Apr 26, 2016 at 10:51 AM 0
Share

Did you read what i said ? I said unless you have designed a singleton approach. He needs to do that.

avatar image RakshithAnand korbul · Apr 26, 2016 at 10:58 AM 0
Share

If you do not know about singletons, then please do read about it before downvoting on an answer straight away. And FYI, for this case, there needs to be only ONE CameraStalkPlayer script.

avatar image korbul RakshithAnand · Apr 26, 2016 at 11:29 AM 0
Share

I am well aware of what a singleton is. And in this case, this design pattern is not needed. You say there needs to be only one camera stalk player. What if OP wants local multiplayer at some point. If you use static, the functionality will not accommodate multiple stalkers with multiple cameras.

Also I don't understand what you mean with "unless you have designed a singleton approach". A singleton is a static instance at it's core, were you aware of that?

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating a camera halts my script? 0 Answers

Needs help to instantiate camera as child of player 0 Answers

How to use 2 Character Controllers in a scene 1 Answer

how can I point a ball rolling, given a force to a rigid body, in line to the camera orientation? Sorry i am a beginner 0 Answers

Instantiating large planes to surround 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