- Home /
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
}
}
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?
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;
}
Just going to try this now, when you say to create the public CSP do you mean a GameObject?
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.
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;
}
// .....
}
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.
Did you read what i said ? I said unless you have designed a singleton approach. He needs to do that.
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.
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?