How to make spawned in game objects pause with mouse click.
Hi, Ive got three columns spawning in and I want one column to pause for 5 seconds when i use GetMouseButtonDown. Any help would be amazing! Thanks.
These are the three columns of squares spawning in:
this is my main script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Main : MonoBehaviour {
public static float Range;
public GameObject[] squaresDownArray;
public GameObject[] squaresUpArray;
public GameObject square;
int index;
public Color[] colourArray;
public Vector2 leftSpawnPosition;
public Vector2 midSpawnPosition;
public Vector2 rightSpawnPosition;
public float spawnTimer;
private float spawnCounter;
private int squareIndexCounter;
// Use this for initialization
void Start () {
//squaresDownArray = GameObject.FindGameObjectsWithTag ("SquareDown");
//squaresUpArray = GameObject.FindGameObjectsWithTag ("SquareUp");
InvokeRepeating ("TimerTick", 0, 1);
}
// Update is called once per frame
void Update () {
spawnCounter += Time.deltaTime;
}
public void TimerTick(){
if(spawnCounter >= spawnTimer){
GameObject lSquare = Instantiate (square, leftSpawnPosition, Quaternion.identity);
lSquare.tag = "SquareDown";
//lSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
lSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray[Random.Range(0,colourArray.Length)];
squareIndexCounter++;
if (squareIndexCounter >= colourArray.Length) {
squareIndexCounter = 0;
}
GameObject mSquare = Instantiate (square, midSpawnPosition, Quaternion.identity);
mSquare.tag = "SquareUp";
//mSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
mSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray[Random.Range(0,colourArray.Length)];
squareIndexCounter++;
if (squareIndexCounter >= colourArray.Length) {
squareIndexCounter = 0;
}
GameObject rSquare = Instantiate (square, rightSpawnPosition, Quaternion.identity);
rSquare.tag = "SquareDown";
//rSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
rSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray[Random.Range(0,colourArray.Length)];
//[squareIndexCounter];
squareIndexCounter++;
if (squareIndexCounter >= colourArray.Length) {
squareIndexCounter = 0;
}
spawnCounter -= spawnTimer;
}
foreach (GameObject square in GameObject.FindGameObjectsWithTag("SquareUp")) {
square.GetComponent<SquareScript> ().Move ("up");
}
foreach (GameObject square in GameObject.FindGameObjectsWithTag("SquareDown")) {
square.GetComponent<SquareScript> ().Move ("down");
}
}
}
this is my script for my squares:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SquareScript : MonoBehaviour {
public float moveSpeed;
private float moveTimeCounter;
public float moveDistance;
private Vector3 endPos;
private float averageDTime;
private int averageDTCounter;
private bool moving;
Main Main;
// Use this for initialization
void Start () {
Main = Main.GetComponent<Main> () as Main;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
moving = false;
}
if (transform.position.y > 8 || transform.position.y < -8) {
Destroy (gameObject);
}
if (moving) {
if (moveTimeCounter <= 1) {
moveTimeCounter += Time.deltaTime * moveSpeed;
averageDTime += Time.deltaTime;
averageDTCounter++;
} else {
moveTimeCounter = 0;
moving = false;
print (averageDTime / averageDTCounter);
averageDTCounter = 0;
averageDTime = 0;
}
transform.position = Vector3.Lerp (transform.position, endPos, moveTimeCounter);
}
}
public void Move(string moveDirection){
moving = true;
if (moveDirection == "up") {
endPos = new Vector3 (transform.position.x, transform.position.y + moveDistance, 0);
}
if (moveDirection == "down") {
endPos = new Vector3 (transform.position.x, transform.position.y - moveDistance, 0);
}
}
}
and this is the mouse down script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ClickScript : MonoBehaviour {
public float speed = 0.1F;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0))
Debug.Log ("Pressed left click.");
if (Input.GetMouseButtonDown (1))
Debug.Log ("Pressed right click.");
if (Input.GetMouseButtonDown (2))
Debug.Log ("Pressed middle click.");
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
// Move object across XY plane
transform.Translate (-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
Debug.Log ("swipe.");
}
}
}
Your answer
Follow this Question
Related Questions
Inconsistent GetMouseButtonDown Behaviour 1 Answer
columns wont pause on mouseclick 0 Answers
Endless Runner with new Biome each time 0 Answers
How to program a pause/unpause button in C#. 2 Answers
How to pause an animation 1 Answer