#Hello I have watched a youtube tutorial
1 messages · Page 1 of 1 (latest)
GameManager.cs:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject block;
public float maxX;
public Transform spawnPoint;
public float spawnRate;
bool gameStarted = false;
public GameObject tapText;
public TextMeshProUGUI scoreText;
int score = 0;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && !gameStarted)
{
StartSpawning();
gameStarted = true;
}
}
void StartSpawning()
{
InvokeRepeating("SpawnBlock", 0.5f, spawnRate);
}
void SpawnBlock()
{
Vector3 spawnPos = spawnPoint.position;
spawnPos.x = Random.Range(-maxX, maxX);
Instantiate(block, spawnPos, Quaternion.identity);
score++;
scoreText.text = score.ToString();
tapText.SetActive(false);
}
}
player.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (touchPos.x < 0)
{
rb.AddForce(Vector2.left * moveSpeed);
}
else
{
rb.AddForce(Vector2.right * moveSpeed);
}
}
else
{
rb.velocity = Vector2.zero;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "block")
{
SceneManager.LoadScene("Game");
}
}
}