How to spawn a player and a camera together?
Hello there!
I've just recently started to try to develop games and stuff, and i have been wondering how can i spawn a camera that actually follows my also spawned player. I've managed to spawn a player, but i can't figure out how to attach a camera to it, because if i simply attach the player prefab together with the camera (or cm) prefab, they just don't work out.
My player is spawned together with my map, with both of them being generated by an image, where it's pixels are converted to blocks prefabs.
Is there any kind of coding (C#) or Unity tool that can do the trick?
I've also tried the following code, but it doesn't seems to be working properly and i'm honestly kinda lost on what to do with it, since i used this video as a base to build my code. Anyway, here's the code. Thanks in advance!
using UnityEngine;
public class TargetPlayer : MonoBehaviour
{
public Transform target;
void Update()
{
FindPlayer();
transform.LookAt(target);
}
void FindPlayer()
{
GameObject findPlayer;
findPlayer = GameObject.FindGameObjectWithTag("Player");
target = findPlayer.transform;
}
Answer by frpm1001 · Aug 02, 2019 at 06:19 AM
To everyone following this question or to whoever sees it, i managed to fix it. It still isn't as smooth as it would be with a CM, but it's working atleast.
If you know how to smooth the camera movement, please let me know! Thanks in advance!
I created an Empty Object, and then made the Camera and the Player it's children. Then, I added a "Player" tag do the Player and created the camera script as it is below. I'd also recommend you to set the offset to a negative value (of atleast -10) so that the camera isn't attached to your player. And then, i made the Empty Object my Prefab!
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform target;
public Vector3 offset;
void Update()
{
FindPlayer();
transform.LookAt(target);
transform.position = target.position + offset;
transform.rotation = Quaternion.Euler(Vector3.zero);
}
void FindPlayer()
{
GameObject findPlayer;
findPlayer = GameObject.FindGameObjectWithTag("Player");
target = findPlayer.transform;
}
}
Answer by ahakra · Aug 01, 2019 at 10:53 PM
attach this script to the camera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class followcomponent : MonoBehaviour {
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update () {
transform.position = player.transform.position + offset;
}
}
the offset to keep a distance off player if you needed it
Hello there! Thanks for the quick reply!
Apparently it didn't work! I believe that it would work if you had your Player on the editor, but in my case, the player is spawned once the game has started!
Your answer
Follow this Question
Related Questions
I'm having issues with collision and Physics 1 Answer
Rotate player to camera direction when using cinemachine FreeLook Camera 0 Answers
Fixing camera for when player is oriented upside down or otherwise. 0 Answers
Trying to connect camera movement with player AND mouse 0 Answers
Camera flickers while chasing the player 0 Answers