Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is Roll-A-Ball tutorial outdated?

Discussion in 'Scripting' started by iDontLikeHipsters, Feb 18, 2015.

  1. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24
    I'm following the camera movement tutorial word-for-word, and I've even tried my own algorithms, but for some reason, I just cannot get the follow C# code to work:


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraBehavior : MonoBehaviour
    6. {  
    7.     public GameObject player;
    8.     private Vector3 offset;
    9.  
    10.     //y
    11.     void Start ()
    12.     {
    13.         offset = transform.position;
    14.     }
    15.  
    16.     // y
    17.     void LateUpdate ()
    18.     {
    19.         transform.position = player.transform.position + offset;
    20.     }
    21. }
    22.  

    The camera will simply stay in the same position while the player moves without it.


    EDIT:

    Also, the player is NOT parented to the camera, and I'm certain "player" is a reference to the GameObj, which is a sphere.
     
  2. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    Everything looks good to me. Have you added the CameraBehavior to the camera gameObject in your scene, and also, have you assigned the Player gameObject in the inspector?
     
  3. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    "LateUpdate()" is only called if it's enabled (http://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html)... try using regular "Update()" and if that doesn't work, tell us.

    Also, like gtzpower said, you need to make sure that the variable "Player" is filled in the inspector.
     
  4. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24
    Yeah, "player" is a reference to my Player game object.

    Also, I need lateUpdate() because the player moves on FixedUpdate(). :p Don't wanna run into jerky camera controls.
     
  5. iDontLikeHipsters

    iDontLikeHipsters

    Joined:
    Mar 24, 2014
    Posts:
    24
    Well, uhhh....

    This is embarrassing.


    For some reason, I named my CUBE "Player" instead of my sphere. Thanks for the help, guys. :p I'll try to pay better attention next time.
     
  6. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    EDIT: posted at the same time as your solution. Glad you figured it out!

    In that case, I would try adding this into the FixedUpdate() function:
    Code (CSharp):
    1. Debug.Log(player.transform.position)
    This will tell us if the LateUpdate() function is running at all, and if it is, we will be able to see if the position of the player is changing as read by the LateUpdate() function.