- Home /
Camera following player without rotation
I am on Unity's tutorial 3 on making a roll-a-ball game but my camera zooms in way too much when I press play. In the video, the camera stays in its original position but it seems that my camera moves to the position of the ball even though I used the exact same code in the video. Here is the code:
using UnityEngine; using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void start() {
offset = transform.position;
}
void LateUpdate() {
transform.position = player.transform.position + offset;
}
}
If this is the right code, it is assu$$anonymous$$g that before the game is run, the 'player' is at Vector3.zero and that the camera is at the proper distance from the ball. A better solution would be to change line 5 to:
offset = transform.position - player.position;
I have tried that and got the following error:
Assets/Scripts/CameraController.cs(10,54): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
position' and no extension method position' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Answer by Chapi · Oct 23, 2014 at 01:21 AM
I can see a lil error in your code, the start function you typed start() and not Start() S must be uppercase if you're using the built in function. And also, if you're trying to use what robertbu said, you must use player.transform.position, not player.position that's why you get the error.
player is a gameObject, which contains the component Transform, and the Transform component contains a member variable called position, that's why you can't access it directly with player.position
Thanks! Turns out the only thing was that start() was lowercase, works perfectly now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613165730im_/https://answers.unity.com/themes/thub/images/avi.jpg)