- Home /
Trying to set multiple camera
Hi there is my code that i attached on the player :
public Camera playerCamera;
public Camera playerDeadCamera;
void Start () {
ShowPlayerView();
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Enemy")
{
ShowPlayerDeadView();
}
}
public void ShowPlayerView()
{
playerCamera.enabled = true;
playerDeadCamera.enabled = false;
}
public void ShowPlayerDeadView()
{
playerDeadCamera.enabled = true;
playerCamera.enabled = false;
}
PlayerCamera have a smooth follow script which targeting Player. I attached these camera to the Player script. That works on MonoBehaviour ofc but when i try to apply this for a multiplayer script that tell me there isnt camera rendering. Can you help me please
By applying it to a mulitplayer script, do you mean you're using 'NetworkingBehaviour'?
Trying to understand specifically what's happening, cause you haven't given much explanation around the error other than 'Cameras not rendering' (which I'm assu$$anonymous$$g happens when you press play). Does this happen immediately when you press play? Are there only two cameras? Only one player present?
Answer by Codez · Dec 14, 2017 at 11:06 PM
I believe the problem comes in with the 'spawning prefabs' part. For the camera to follow a spawned player, it will need to locate the player object first. I would suggest looking at this Unity Tutorial https://unity3d.com/es/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera Hope this helps :)
I cant found, im getting stuck, tell me please if you know how resolve it
@Codez, never end your links with a full stop, it will break. https://unity3d.com/es/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera
Ah, thanks Sublime; I wasn't aware links included those full-stops. It's fixed now.
Answer by ElijahShadbolt · Dec 15, 2017 at 09:15 PM
If you've got two prefabs, one for Player and one for PlayerCamera, you cannot just set the smooth follow to reference the Player prefab. When the Player and PlayerCamera are instantiated in this case, the reference will still be to the Player prefab, not the instantiated Player object.
You will need to create a custom script, and when the Player and PlayerCamera are instantiated, assign the smooth follow reference to the newly created Player instance.
Or if you have the prefabs already instantiated in the scene in Edit mode, in the smooth follow Inspector assign the reference to the Scene objects, not the prefabs.
Thanks !
Do you think this script can work or not ? ( I try this tomorrow )
Camera.mainCamera.GetComponent(SmoothFollow).target = player;
Yeah, it sounds like your on the right track. I got something similar working and hopefully it does for you too. in your player controller script, try adding: public override void OnStartLocalPlayer() { Camera.main.GetComponent().setTarget(gameObject.transform); } And then in the Update function in the 'SmoothFollow' script, add this: public void setTarget(Transform target) { playerTransform = target; } (https://answers.unity.com/questions/1157437/making-my-camera-follow-player-in-multiplayer.html)
You'll need to edit the SmoothFollow script (in project folder Assets > Standard Assets > Utility) and change private Transform target;
to
public Transform target;
Then when you instantiate 'player', you could use
Camera.main.GetComponent<SmoothFollow>().target = player.transform;
Answer by programer717 · Dec 16, 2017 at 04:07 AM
@imM4TT the cameras need to be on the same render to be able to switch between cameras make sure you set one to enabled to false and one to true I don't see any thing wrong with your script at first glance.
Thanks you all, that's working now =) I just need to adjust the camera settings and it's right
Answer by imM4TT · Dec 14, 2017 at 03:11 PM
The problem is that if I just set my playerCamera as childrens of my player, the smooth follow isnt working. So the camera is following the player but the smooth follow script is like deactivated. And when I try to attach the cameraPlayer ( with smooth follow script ) to the Player by the inspector, this isnt working. I need help please. Ill post a " solution " if i found ...
Your answer
Follow this Question
Related Questions
Multiplayer| Attaching camera to player (if i am the owner 1 Answer
Network Manager GUI 1 Answer
Network Moving Help. 1 Answer
On Network GameObject Movment and Rotation Problem. 1 Answer
Problem with camera and gun in FPS game 0 Answers