- Home /
Question by
Morear · Apr 21, 2015 at 02:22 PM ·
camera follow
I can't move camera with my character.
This is my first time using Unity and I stuck at very basic. I can't move camera with my character. I follow one Youtuber's videos, Quill18. I enter same code with him. But result is not the same. Here is the code:
using UnityEngine;
using System.Collections;
public class CameraTracksPlayer : MonoBehaviour {
Transform player;
float offsetX;
Vector3 pos;
// Use this for initialization
void Start () {
GameObject player_go = GameObject.FindGameObjectWithTag("Player");
if(player_go == null) {
Debug.LogError("Couldn't find an object with tag 'Player'!");
return;
}
player = player_go.transform;
offsetX = (transform.position.x - player.position.x);
}
// Update is called once per frame
void Update () {
if(player != null) {
pos = transform.position;
pos.x = (player.position.x + offsetX);
transform.position = pos;
}
}
}
By the way, I have tried several different methods from other people's Q&As but nothing worked.
Comment
I don’t see any code to move the camera, also specify what the problem and expected behaviour is. No on is going to watch the videos for you.
A simple way to do that is just to set your camera gameObject as a children of your character, it will move with him :)
Answer by hbalint1 · Apr 21, 2015 at 09:47 PM
pos = transform.position;
you sure it's not
pos = player.position;
?