New and lost

Hey there, I’m Mel.

This is my first time using Unity, and I’ve just completed the Roll-a-Ball game tutorial I’ve found on Youtube, which came just in handy for a little game I was trying to put together. My only problem is, when I try to customize the scripts used in the tutorial to fit my needs a bit more, namely the camera, I get completely stuck.

Here’s the script:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

    //Visible, invisible in the menus / Variable Type / Variable name
    public GameObject player;
    private Vector3 offset;
    //GameObject is for which game object to assign as the player
    //Vector3 refers to the three axises

    // Use this for initialization
    void Start () {
        offset = transform.position - player.transform.position;

    }
   
    // Update is called once per frame
    void LateUpdate () {
        transform.position = player.transform.position + offset;

    }
}

(yes i make notes for myself so i don’t get confused)

Right now, the camera follows the rolling ball perfectly behind it, however, all I want to do is the make it stop moving vertically, meaning if the ball goes left or right, the camera would not follow it just from the center of the track. I tried to look up on something that might help in the documentation but I don’t even know what I should be looking for.

I know this might be a stupidly easy thing for some of you here, but for me, this is a bit of getting thrown into deep water. Any help is appreciated.

Hey,

Welcome to Unity!

The easiest thing to do would be to store the positions you want to follow in a vector 3 and ignore the others.

using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
    //Visible, invisible in the menus / Variable Type / Variable name
    public GameObject player;
    private Vector3 offset;
    //GameObject is for which game object to assign as the player
    //Vector3 refers to the three axises
    // Use this for initialization
    void Start () {
        offset = transform.position - player.transform.position;
    }
  
    // Update is called once per frame
    void LateUpdate () {
        Vector3 playerposVert = new Vector3(player.transform.position.x + offset, 0,0);

        transform.position = playerposVert ;
    }
}

You would choose which axis to keep in the vector 3, x,y or z. (i’m not sure which way you’re moving ^^)
Let me know if this is what you mean :smile:

I tried loading it but I get an error that says: “Operator ‘+’ cannot be applied to operands of type ‘float’ and ‘Vector3’”.
Which I guess means that you can’t do any kind of math when defining Vector3 co-ordinates.

I tried turning the (player.transform.position.x + offset, part into a variable on its own, but it did not work.

Sorry, yes I added a v3 to a float, you should do (player.transform.position.x + offset.x

do this, lots, really is a good habit to get into :smile:

1 Like

It worked perfectly. Thank you so much!

1 Like