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 SpiderManL · Nov 30, 2021 at 12:27 AM · cameraonline

How to make Camera follow clone player ?,How to make Camera follow cloned object?

My game is set up that when a player joins the game a character will spawn here is that script:

public class SpawnPlayers : MonoBehaviour { public GameObject playerPrefab;

 public float minX;
 public float minY;
 
 public float maxX;
 public float maxY;
 

 private void Start()
 {
     Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
     PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);

 }

}

my camera script works like this: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Camera_fllow : MonoBehaviour { public Transform target; public Vector3 offset;

 // Update is called once per frame
 void LateUpdate()
 {
     transform.position = target.position + offset;
 }

}

is there any way that i can make my camera's "target" to the character my script spawns?,In my game the characters are created by this script when a player joins the game:

using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun;

public class SpawnPlayers : MonoBehaviour { public GameObject playerPrefab;

 public float minX;
 public float minY;
 
 public float maxX;
 public float maxY;
 

 private void Start()
 {
     Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
     PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);

 }

}

My camera following script is set up like this: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Camera_fllow : MonoBehaviour { public Transform target; public Vector3 offset;

 // Update is called once per frame
 void LateUpdate()
 {
     transform.position = target.position + offset;
 }

}

Is there any way I can set "target" to the player the SpawnPlayers script creates ?

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 AaronBacon · Nov 30, 2021 at 01:56 PM

Yep, the easiest solution would just be to make a static reference to the Camera flow script, then just have the same line that spawns the player store it in a variable so you can set the target to it, like so:

⠀

Camera Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Camera_fllow : MonoBehaviour 
 {
     public Transform target;
     public Vector3 offset;
     public static Camera_fllow mainCam;
     
     // Awake is called when the script instance is being loaded.
     void Awake()
     {
         mainCam = this;
     }
     
     // Update is called once per frame
     void LateUpdate()
     {
         transform.position = target.position + offset;
     }
 }

⠀

And then on the Player Spawn script:

⠀

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun;
 
 public class SpawnPlayers : MonoBehaviour
 { 
     public GameObject playerPrefab;

 public float minX;
 public float minY;

 public float maxX;
 public float maxY;

 private void Start()
 {
     Vector2 randomPosition = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
     GameObject newPlayer = PhotonNetwork.Instantiate(playerPrefab.name, randomPosition, Quaternion.identity);
     Camera_fllow.mainCam.target = newPlayer.transform;
 }
 }

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

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

Spectator Camera (Online) 0 Answers

Multiplayer Cameras First Person One Camera 0 Answers

Network multiplayer chess/battleship game cameras 2 Answers

Multiplayer camera bug - Everyone sees what last joinen player sees. 0 Answers

My Camera won't activate trough script 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