Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Does anyone know why this simple player camera lock script is not working?

Discussion in 'Scripting' started by PresidentPorpoise, Feb 5, 2017.

  1. PresidentPorpoise

    PresidentPorpoise

    Joined:
    Nov 6, 2016
    Posts:
    37
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraLock : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.  
    9.     private Vector3 offset;
    10.     private Vector3 cameraPos;
    11.     private Vector3 playerPos;
    12.  
    13.     private void Awake()
    14.     {
    15.         cameraPos = transform.position;
    16.         playerPos = player.transform.position;
    17.     }
    18.  
    19.     private void Start()
    20.     {
    21.         offset = cameraPos - playerPos;
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         cameraPos = playerPos + offset;
    27.     }
    28. }
    29.  
    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.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    This should work (I don't understand why you have the Awake and Start methods, when their content can easily go into the Start only, but it should work) Are you sure you're placing the script onto the camera? Did you assign the player gameobject to the player variable?
     
  3. PresidentPorpoise

    PresidentPorpoise

    Joined:
    Nov 6, 2016
    Posts:
    37
    Yes, I did all of that. I moved the assignments to Awake when I attempted to troubleshoot. This is the version that works:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraLock : MonoBehaviour {
    6.  
    7.     public GameObject player;
    8.  
    9.     private Vector3 offset;
    10.     //private Vector3 cameraPos;
    11.     //private Vector3 playerPos;
    12.  
    13.     private void Start()
    14.     {
    15.         // cameraPos = transform.position;
    16.         // playerPos = player.transform.position;
    17.         offset = transform.position - player.transform.position;
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         transform.position = player.transform.position + offset;
    23.     }
    24. }
    25.  
     
    Last edited: Feb 5, 2017
  4. original-name

    original-name

    Joined:
    Aug 5, 2016
    Posts:
    3
    Since cameraPos and playerPos values are assigned in start / awake, they arent updating. Simply assigning them again in the update function would (should) work, or you could do something like this :

    Code (CSharp):
    1.     public GameObject player;
    2.  
    3.     private Vector3 offset;
    4.     private Transform cameraTrans;
    5.     private Transform playerTrans;
    6.  
    7.  
    8.     private void Start()
    9.     {
    10.         offset = cameraTrans.position - playerTrans.postion;
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         cameraTrans.position = playerTrans.position + offset;
    16.     }
    I think.
     
  5. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    In addition to what original-name stated you will probably want to use late update.

    void LateUpdate()

    as this will allow all change to the player object to take effect before moving the camera.
     
unityunity