Issue with ball tutorial.

I’m following the roll a ball game tutorial. Everything was working fine until it came to the camera. To start, for some reason my camera was on ortho mode, so I changed it to perspective.

I can see the preview of the camera in editor mode, and it shows the right perspective of the ball on the plane. But once I add the camera control script into the camera, and set the player as the focus point. When I hit play it shows only a zoomed in shot of the plane and I can’t see the ball anywhere.

Before play: Screenshot - 4c055895ed4137dbee9e4f2cad02e986 - Gyazo
After play: Screenshot - 2549fa087345bc922d9acb384c67770e - Gyazo

This is my code.

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

public GameObject player;

private Vector3 offset;

// 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;
}
}

//// Also just want to make sure I understand this.

" offset = transform.position = player.transform.position; "

so offset is the variable, but when you type offset = transform.position
Does that mean you are setting variable as a vector3 position, or that you are accessing the position property and then setting offset as the player’s position value?

Let me know if that doesn’t make sense, i’ll try to word it better. Thanks!

Please use code tags for code, it makes it easier to read.

Your code makes no sense at all. In LateUpdate, you’re setting the camera’s position to the player’s position, you’re setting it to “offset”. “offset” is the player’s position. You clearly have no idea on what you’re doing.

What you probably want is something like this:

public Vector3 Offset;
public Transform PlayerObject;
void LateUpdate()
{
    transform.position = PlayerObject.position + Offset;
}

In the Inspector you can set the Offset.

No need to be so harsh. Clearly the OP is a beginner and obviously a bit lost. Maybe a little empathy is required on your part.

All that part does is set the camera’s position to exact player’s position, and then stores that position in offset. Offset really serves no purpose the way you’re using it in your code. Follow the above suggestion by DroidifyDevs e.g.

transform.position = player.transform.position + offset;

Also, the reason why you don’t see the ball when the game starts is because the camera is inside the ball.

Yeah I wrote that quickly and it did come off a bit harsh, so that’s my bad :confused:

However OP is doing the right thing by doing the tutorials, and the code I provided should help him out. Wishing him the best of luck :slight_smile:

Oh I accidentally put a = offset instead of adding the offset. My bad haha.

That won’t help you because you aren’t setting your offset correctly. You’re setting the offset to the player’s start position, then you’re adding that to the player’s position every frame of the game. You’re going to get some very strange results with that.

My code should work OK.

I did
offset = transform.position - player.transform.position;

and transform.position = player.transform.position + offset; and it’s working now.

1 Like