Trying to get camera to look at brick wall

So I’m a bit of a newbie at Unity but I’m going through the Documentation and at the section - Creating gameplay it tries to show off prefabs a bit by making you create a brick wall which I assume will be destructible.

However when I press play the brick flies down past the camera so I can’t see this brick wall. I thought to myself how about I make a lookAt function like I’ve done before.

I copied the code from the Scripting reference for the lookAt function however the camera doesn’t follow the brick as it falls down in the game world. I’m not sure why the brick is falling because it doesn’t have a Rigid Body attached to it either.

The code for the brick wall creation attached as a component to an empty game object called Blocks with the tag of my user-created tag, Wall.

#pragma strict

function Start () {
    for (var y = 0; y < 5; y++) {
        for (var x = 0; x < 5; x++) {
            var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.AddComponent.<Rigidbody>();
            cube.transform.position = Vector3 (x, y, 0);
        }
    }
}

function Update () {

}

The code attached to the main camera with the target in the inspector being Blocks.

#pragma strict

public var target: Transform;

function Update() {
    // Rotate the camera every frame so it keeps looking at the target
    transform.LookAt(target);
}

Thanks. :slight_smile:

Use the “Insert” widget (between the movie frames and the floppy disk icon in the editor toolbar) to insert code, and then paste in your whole script. Also explain how (or whether?) you attached this script to the camera, and set its public properties in the inspector.

Then we’ll be able to help you. :slight_smile:

Okay I updated my original post. Well from this I’ve already seen I’m making all my cubes rigid bodies. :stuck_out_tongue: I just blindly thought they weren’t rigid bodies because it wasn’t appearing as a component in the inspector.

Ah, OK. I think I see the problem: you’ve set up the camera to look at your empty Blocks object. That’s not the one that’s falling… it’s only there to run the code that creates all the blocks (which are entirely separate objects).

Because the blocks are being created in code, setting your camera up to follow one of them will be a bit of a pain. I suggest this: delete that empty object with the block-creation code on it, and instead create your brick wall manually. Yes, this is a little tedious, but it’s also good practice. You can just create one, add a rigidbody, size it how you like (make it more brick-shaped!), and then duplicate it several times to make a row of them. Then you can select all the blocks in the row, and duplicate the whole set to produce the next row, etc.

Then you can set your camera to look at one of the blocks, and it should work.