- Home /
The question is answered, right answer was accepted
Control when instantiated objects can move.
Hello. I am making a clone of the "Don't touch the white tile game" and it's all running smoothly, but I seem to be stuck on trying to limit which "black" tile moves them all down. I want it to be only when I touch the black block on the bottom that executes the command to move them all.
Currently when I touch any black block it'll move it down. I need some type of method to prevent anything higher than 0f cannot execute the "move" command. using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Movement : MonoBehaviour {
public Instantiate bringInstantiate = new global::Instantiate ();
private SoundFX importSound = new SoundFX ();
private ScoreLog importScore = new ScoreLog();
private string importScorelog = ScoreLog.currentScore.ToString();
private int touchCounter;
private GameObject[] grabBlackClonesArray;
private Vector3[] vectors = new Vector3[4];
private Vector3 selected;
private int randomNumber;
private bool correctTouch;
public void Awake()
{
//Create vector Positions
this.vectors [0] = new Vector3 (0f, 3.75f, -1.5f);
this.vectors [1] = new Vector3 (1.25f, 3.75f, -1.5f);
this.vectors [2] = new Vector3 (2.5f, 3.75f, -1.5f);
this.vectors [3] = new Vector3 (3.75f, 3.75f, -1.5f);
}
void Start()
{
//We set the array to have slots 0,1,2,3 (total of 4)
grabBlackClonesArray = new GameObject[5];
//We debug to see if the position 0 is not null
Debug.Log (grabBlackClonesArray[0]);
}
void Update() {
int randomNumber = Random.Range (0, 4);
this.selected = this.vectors [randomNumber];
importScorelog = ScoreLog.currentScore.ToString();
//Vector3[] newlocation =
//We finally set grabBlackClonesArray and fill it up with all objects
//with the tag "BlackTile"
grabBlackClonesArray = GameObject.FindGameObjectsWithTag("BlackTile");
//We check to see if it is true
//To make sure that the movement is only done once we put it in a loop
//remember we are in the Update method..it's called every frame
if(grabBlackClonesArray[0].transform.position.y <= -1.25f) {
Debug.Log ("clicked and ran <= 0.f");
grabBlackClonesArray[0].transform.position = selected;
}
if(grabBlackClonesArray[1].transform.position.y <= -1.25f) {
Debug.Log ("clicked and ran <= 0.f");
grabBlackClonesArray[1].transform.position = selected;
}
if(grabBlackClonesArray[2].transform.position.y <= -1.25f) {
Debug.Log ("clicked and ran <= 0.f");
grabBlackClonesArray[2].transform.position = selected;
}
if(grabBlackClonesArray[3].transform.position.y <= -1.25f) {
Debug.Log ("clicked and ran <= 0.f");
grabBlackClonesArray[3].transform.position = selected;
}
//We need to grab the touch on Android Devices
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetKeyDown ("k")) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray,out hit) && hit.collider.gameObject.name == "BlackTile(Clone)")
{
importScore.Score ();
importSound.SoundFXs();
correctTouch = true;
grabBlackClonesArray [1].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [0].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [2].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [3].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [4].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
}
else if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
touchCounter++;
if(touchCounter == 1) {
Application.LoadLevel(2);
touchCounter--;
}
}
}
}
void OnGUI() {
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(4, 2, 1));
GUI.Label (new Rect (9,125,255,255), (importScorelog));
}
}
/////////////////////////////
Answer by ObeliskEntertainment · Jun 01, 2014 at 12:16 AM
Wow, I was quick to solve my own problem. I just needed to put a if statement with the movement command. Then after that I needed to use a raycast to detect which object was hit and check it's location.
if(grabBlackClonesArray[3].transform.position.y == 0f) {
if(Physics.Raycast (ray,out hit) && hit.collider.gameObject.transform.position.y == 0f) {
grabBlackClonesArray [1].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [0].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [2].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [3].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
grabBlackClonesArray [4].transform.position -= new Vector3 (0.0f, 1.25f, 0.0f);
}
}
Follow this Question
Related Questions
Movement through an array over time 2 Answers
Instantiated object isnt moving 1 Answer
Problem with moving objects in array? 2 Answers
Move continuously through array of Vectors 1 Answer
NullRefenceException Using Arrays 2 Answers