- Home /
Does anyone know why this simple player camera lock script is not working?
Hello, I am making a simple 2D game and I am trying to lock the camera to the player using a C# script. The script works when I do not put the positions of the player and camera into Vector3 variables, but when I do the camera does not lock to the player and I do not see any errors appear in the console. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLock : MonoBehaviour {
public GameObject player;
private Vector3 offset;
private Vector3 cameraPos;
private Vector3 playerPos;
private void Awake()
{
cameraPos = transform.position;
playerPos = player.transform.position;
}
private void Start()
{
offset = cameraPos - playerPos;
}
private void Update()
{
cameraPos = playerPos + offset;
}
}
Can anyone tell me what I am doing wrong? Am I using the wrong variable types for storing the x,y, and z values of the positions? Thanks in advanced.
Follow this Question
Related Questions
Smooth camera script looking at left hand side of character. 0 Answers
Need help fixing my script 1 Answer
How to add force if spherecast is true and how to set direction of sphere 0 Answers
How do I make a photography function? 0 Answers
How do I make my projectile deal damage to a target it hit. 1 Answer