- Home /
Moving an Object to the vector of other objects on button press using a vector3 array?
OK! so. I'm currently working on a game where I there are multiple places for a user to place a game piece. Sort of like chess. Because it's in 3D I am struggling with the having set locations for the game pieces to be placed and so far what I've got is code that is supposed to place a marker at the currently selected location and switch to the next available location on button press.
I have placed invisible squares (just with the mesh toggled off) on the game board and have given them all the tag "space" my code is written to first take them into a GameObject array then I've created a loop that goes through each game object (these are selected using the "space" tag) and it puts their Vector3 values into another array that I can call each space from using each specific index.
This was supposed to call the vector3 at index[x] and then translate.transform my sphere marker object? but it just puts the sphere at 0.0f 0.0f 0.0f...
is there an easier way to do this?? -___-;;;! Ty so much for the help! sorry if this is all crazy I'm only just starting my second year at school for programming so I still know very little about this madness.
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // <--- for button behavior
public class CreateSprite : MonoBehaviour {
public Button myButton;
public Vector3[] characterSpacePostion = new[] { new Vector3() };
// create an int to know how many times you've pushed this button
public int buttonPress = 0;
// Use this for initialization
void Start () {
// create an array for each object
GameObject[] characterSpaceOptions = GameObject.FindGameObjectsWithTag("space");
// create an array for each position
// sort through each objects with the tag "space"
for (var i = 0; i < characterSpaceOptions.Length; i++)
{
// get the currently selected objects position
Vector3 nextVect = characterSpaceOptions[i].transform.position;
// add the curerrent objects vector 3 to the array at the same position
characterSpacePostion[i] = new Vector3(nextVect.x, nextVect.y, nextVect.z);
}
}
// Update is called once per frame
void Update()
{
// find all objects with the tag "space"
myButton = GetComponent<Button>(); // <-- you get access to the button component here
myButton.onClick.AddListener(() => { scrollThroughSpaces("stringValue", 4.5f); }); // <-- you assign a method to the button OnClick event here
// myButton.onClick.AddListener(() => { myAnotherFunctionForOnClickEvent("stringValue", 3); }); // <-- you can assign multiple methods
}
// use a method to find each object with specific tag then
// use a for statement to get each objects location.
void scrollThroughSpaces(string argument1, float argument2)
{
/** // create an array for each object
GameObject[] characterSpaceOptions = GameObject.FindGameObjectsWithTag("space");
// create an array for each position
Vector3[] characterSpacePostion = new[] { new Vector3() };
// your code goes here
// example ----> print(argument1 + ", " + argument2.ToString());
// sort through each objects with the tag "space"
for (var i = 0; i < characterSpaceOptions.Length; i++)
{
// get the currently selected objects position
Vector3 nextVect = characterSpaceOptions[i].transform.position;
// add the curerrent objects vector 3 to the array at the same position
characterSpacePostion[i] = new Vector3(nextVect.x, nextVect.y, nextVect.z);
}
**/
// get the red sphere that is supposed to go to each location in order
GameObject selectionSphere = GameObject.Find("selectionSphere");
// the button press is the index of the vector3 we need from the vector3 array
// because it starts at 0 and adds one for every button press as seen below.
// this puts the vector3 value at index (buttonpress) into a varable to use here.
Vector3 positionToMoveTo = (characterSpacePostion[buttonPress]);
// this is supposed to put the selection sphere at the location of the vector3
// in the array at index (buttonpress) but it seems to be placing it at 0.0f, 0.0f, 0.0f?
selectionSphere.transform.Translate(positionToMoveTo);
// add one to button press to get the next vector3 out of the array using this as
// an index.
buttonPress++;
// I only have 30 positions saved under the tag "space" at the moment
// so this "should" allow it to loop back through once it's on the last one.
if (buttonPress >= 30)
{
buttonPress = 0;
}
}
//void myAnotherFunctionForOnClickEvent(string argument1, int argument2)
//{
// your code goes here
// print(argument1 + ", " + argument2.ToString());
//}
}
I don't have time to code an example of something like this. However, I did find this tutorial that may give you an idea.
Your answer
Follow this Question
Related Questions
Instantiated object isnt moving 1 Answer
Problem with a motion script 0 Answers
Sum Of Two Motions? 0 Answers
Check if 2 objects positions are close enough 2 Answers
Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer