How do I assign MainCamera to a dynamically created primitive?
Hi all. I created a sphere by making an empty GameObject and then used CreatePrimitive to make the sphere.
Then I used the PlayerController script from the Roll-a-Ball example.
And I used the CameraController script from Roll-a-Ball, but I can't get the camera to follow the sphere.
I set the tag to "myplayer" in the tag manager, and tried to link it to CameraController with FindWithTag("myplayer"). I get no errors (finally), but the camera (MainCamera) will not follow the sphere. The question is, how to assign MainCamera to a dynamically created primitive?
This is the complete code...
public class CreateSphere: MonoBehaviour { void Start() { GameObject myplayer = GameObject.CreatePrimitive(PrimitiveType.Sphere); myplayer.tag = "myplayer"; myplayer.AddComponent(); myplayer.AddComponent(); } }
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
public float speed=10;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start()
{
player = GameObject.FindWithTag("player");
offset = transform.position - player.transform.position;
Debug.Log(offset);
}
void LateUpdate()
{
transform.position = player.transform.position + offset; //To make camera follow mouse
}
}