#After I eat I will send them
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");
}
}
}
Block.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (transform.position.y < -6f)
{
Destroy(gameObject);
}
}
}
@still prawn
You could make a separate script for the spawner that has a timer.
In update you add to timer the time.delta
If timer>=spawnrate
then spawn and timer-=spawnrate
Found it @rustic cipher. Learn this when you get comfortable with the basics https://gameprogrammingpatterns.com/contents.html
thank you
what did you have to eat?
some paté on bread
idk how to say it in english, it's a romanian thing called "pateu"
@rustic cipher Cu cheia sper