- Home /
Question by
Elisenstein · Sep 21, 2020 at 10:04 PM ·
random.rangemeshrendererintegerupdate problem
How do I randomly change whether a mesh is on or off for an object, based on another object updating?
I have created an integer hand_visible that is supposed to alternate between = 0 or 1 when another object updates, but it only seems to work for the very first time the object updates.
public class GameManager : MonoBehaviour
{
public bool rightHand = false; //false is left, true is right
public GameObject ball;
public GameObject cube;
private bool reset = false; //reset is whether the hand is in the phase of the trial reaching for the ball, or before the next ball has spawned
private int trial = 0;
private float r = 0.4f;
private float theta;
private double error;
private float[] initCoords;
//private UnityEngine.Random rnd = new UnityEngine.Random();
private Vector3 corVec = new Vector3(0.1f, 0.1f, 0.1f);
private GameObject OVRCameraRig;
private GameObject TrackingSpace;
private GameObject LeftHandAnchor;
private GameObject RightHandAnchor;
private GameObject LeftFab;
private GameObject RightFab;
private GameObject Hand;
public static GameManager Instance;
[SerializeField] private OVRSkeleton skeleton;
private OVRHand hand;
private Transform tip;
private int[,] expOrder = new int[25, 2];
private float[,,] data = new float[5, 5, 2];
public int hand_visible;
// Start is called before the first frame update
void Start()
{
if (Instance == null)
{
Instance = this;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
expOrder[i * 5 + j, 0] = i;
expOrder[i * 5 + j, 1] = j;
}
}
Shuffle();
OVRCameraRig = GameObject.Find("OVRCameraRig");
TrackingSpace = OVRCameraRig.transform.GetChild(0).gameObject;
LeftHandAnchor = TrackingSpace.transform.GetChild(4).gameObject;
RightHandAnchor = TrackingSpace.transform.GetChild(5).gameObject;
LeftFab = LeftHandAnchor.transform.GetChild(1).gameObject;
RightFab = RightHandAnchor.transform.GetChild(1).gameObject;
if (rightHand == false)
{
Hand = LeftHand.Instance.gameObject;
skeleton = LeftFab.GetComponent<OVRSkeleton>();
hand = LeftFab.GetComponent<OVRHand>();
RightFab.gameObject.SetActive(false);
}
else
{
Hand = RightHand.Instance.gameObject;
skeleton = RightFab.GetComponent<OVRSkeleton>();
hand = RightFab.GetComponent<OVRHand>();
LeftFab.gameObject.SetActive(false);
}
theta = UnityEngine.Random.Range(110f, 200f) * Mathf.PI / 180f;
theta = ((expOrder[trial, 0] + 1) * 20 + 30) * Mathf.PI / 180f;
Instantiate(ball, new Vector3(r * Mathf.Cos(theta), 0, r * Mathf.Sin(theta)), Quaternion.identity);
tip = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform;
}
// Update is called once per frame
void Update()
{
{
if ((tip.transform.position).magnitude >= r && (reset == false) && trial < 25)
{
error = Vector3.Angle(Ball.Instance.gameObject.transform.position, tip.transform.position);
error = System.Math.Round(error, 3);
reset = true;
Ball.Instance.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
Instantiate(cube, new Vector3(0f, 0f, .2f), Quaternion.identity);
trial++;
hand_visible = Random.Range(0, 2);
{
if (hand_visible == 0)
{
RightFab.GetComponent<OVRMeshRenderer>().enabled = false; //makes hand invisible
}
//else
if (hand_visible == 1)
{
RightFab.GetComponent<OVRMeshRenderer>().enabled = true; //makes hand visible
}
}
}
}
if ((tip.transform.position).magnitude <= .5 * r && (reset == true) && trial < 25)
{
reset = false;
theta = ((expOrder[trial, 0] + 1) * 20 + 30) * Mathf.PI / 180f;
Ball.Instance.gameObject.transform.localScale = new Vector3(0.025f, 0.025f, 0.025f);
Destroy(Cube.Instance.gameObject);
Ball.Instance.gameObject.transform.position = new Vector3(r * Mathf.Cos(theta), 0, r * Mathf.Sin(theta));
}
if (trial >= 25)
{
Ball.Instance.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
}
}
public void Shuffle()
{
int rand; ;
int tempOne;
int tempTwo;
for (int i = 0; i < expOrder.GetLength(0); i++)
{
rand = UnityEngine.Random.Range(0, expOrder.GetLength(0) - 1);
tempOne = expOrder[rand, 0];
tempTwo = expOrder[rand, 1];
expOrder[rand, 0] = expOrder[i, 0];
expOrder[rand, 1] = expOrder[i, 1];
expOrder[i, 0] = tempOne;
expOrder[i, 1] = tempTwo;
}
}
}
Comment