// This is the collision system. private void OnTriggerEnter(Collider other) { if (!other.gameObject.CompareTag("Pickup")) { return; } GetComponent <AudioSource>().PlayOneShot(other.gameObject.GetComponent <PickupSound>().Sound, 10); Destroy(other.gameObject); // Tally the number collected per current block int BlockID = TrialProgress.GetCurrTrial().BlockID; TrialProgress.GetCurrTrial().TrialProgress.NumCollectedPerBlock[BlockID]++; TrialProgress.GetCurrTrial().NumCollected++; E.LogData( TrialProgress.GetCurrTrial().TrialProgress, TrialProgress.GetCurrTrial().TrialStartTime, transform, 1 ); if (--localQuota > 0) { return; } E.Get().CurrTrial.Notify(); _playingSound = true; }
// This function generates the tile floor. We can modify the size of this later. private void GenerateTileFloor() { if (MakeFloor == true) { var val = E.Get().CurrTrial.enclosure.Radius * 2; // Setup the polygon mesh (using sensible defaults). int numSides = E.Get().CurrTrial.enclosure.GroundTileSides == 0 ? 4 : E.Get().CurrTrial.enclosure.GroundTileSides; double tileSize = E.Get().CurrTrial.enclosure.GroundTileSize == 0.0 ? 1.0 : E.Get().CurrTrial.enclosure.GroundTileSize; var col = Data.GetColour(E.Get().CurrTrial.enclosure.GroundColor); Mesh mesh = ConstructTileMesh(numSides, tileSize); // Generate a grid of tiles var xStart = E.Get().CurrTrial.enclosure.Position[0] - val; var yStart = E.Get().CurrTrial.enclosure.Position[1] - val; var xEnd = xStart + (val * 2); var yEnd = yStart + (val * 2); for (float i = xStart; i <= xEnd; i += 2) { for (float j = yStart; j <= yEnd; j += 2) { var tile = Instantiate(Wall, new Vector3(i, 0.001f, j), Quaternion.identity); tile.GetComponent <MeshFilter>().mesh = mesh; tile.GetComponent <Renderer>().material.color = col; tile.transform.localScale = new Vector3(1, 0.001f, 1); // Use the rotate if the pattern looks off //tile.transform.Rotate(0, -45, 0); _created.Add(tile); } } } }
// In start, we call the three initialize functions defined below. private void Start() { var obj = Instantiate(Generator, new Vector3(transform.position.x, 0, transform.position.z), Quaternion.identity); _created = new List <GameObject> { obj }; // The generator is immediately added to the list for destroyed objects SetupColours(); GenerateWalls(); if (E.Get().CurrTrial.enclosure.GroundTileSides == 2) { throw new Exception("Can't have floor tiles with 2 sides!"); } if (E.Get().CurrTrial.enclosure.GroundColor == null) { GameObject.Find("Ground").GetComponent <Renderer>().enabled = false; } else if (E.Get().CurrTrial.enclosure.GroundTileSides > 2) { GenerateTileFloor(); } else { var col = Data.GetColour(E.Get().CurrTrial.enclosure.GroundColor); GameObject.Find("Ground").GetComponent <Renderer>().material.color = col; } GenerateLandmarks(); }
private void ComputeMovement() { // Dont move if the quota has been reached if (localQuota <= 0 & E.Get().CurrTrial.trialData.Quota != 0) { return; } // This calculates the current amount of rotation frame rate independent var rotation = Input.GetAxis("Horizontal") * DS.GetData().CharacterData.RotationSpeed *Time.deltaTime; // This calculates the forward speed frame rate independent _moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); _moveDirection = transform.TransformDirection(_moveDirection); _moveDirection *= DS.GetData().CharacterData.MovementSpeed; // Here is the movement system const double tolerance = 0.0001; // We move iff rotation is 0 if (Math.Abs(Mathf.Abs(rotation)) < tolerance) { _controller.Move(_moveDirection * Time.deltaTime); } transform.Rotate(0, rotation, 0); }
private void Update() { currBlockId = E.Get().CurrTrial.BlockID; currTrialId = E.Get().CurrTrial.TrialID; //HUD for the number of successful trials in the current Block if (DS.GetData().Blocks[currBlockId].ShowNumSuccesses | DS.GetData().Trials[currTrialId].ShowNumSuccesses) { var trialsuccessText = GameObject.Find("TrailSuccesses").GetComponent <Text>(); trialsuccessText.text = "Successful Trials: " + E.Get().CurrTrial.TrialProgress.NumSuccess; } //HUD for the number of goals found in the current trial if (DS.GetData().Blocks[currBlockId].ShowTrialTotal | DS.GetData().Trials[currTrialId].ShowTrialTotal) { var trialtotalText = GameObject.Find("TrialTotal").GetComponent <Text>(); trialtotalText.text = "Goals Found In Trial: " + E.Get().CurrTrial.NumCollected; } //HUD for the number of goals found in the current Block if (DS.GetData().Trials[currTrialId].ShowBlockTotal | DS.GetData().Blocks[currBlockId].ShowBlockTotal) { var blocktotalText = GameObject.Find("BlockTotal").GetComponent <Text>(); blocktotalText.text = "Goals Found In Block: " + E.Get().CurrTrial.TrialProgress.NumCollectedPerBlock[currBlockId]; } }
//Sets the image of the loading screen. public void Start() { Debug.Log("Entering Loading Screen: " + E.Get().CurrTrial.TrialID); var filePath = DataSingleton.GetData().SpritesPath + E.Get().CurrTrial.trialData.FileLocation; GetComponent <RawImage>().texture = Img2Sprite.LoadTexture(filePath); }
// Start the character. If init from enclosure, this allows "s" to determine the start position public void ExternalStart(float pickX, float pickY, bool useEnclosure = false) { while (!_isStarted) { Thread.Sleep(20); } TrialProgress.GetCurrTrial().TrialProgress.TargetX = pickX; TrialProgress.GetCurrTrial().TrialProgress.TargetY = pickY; // No start pos specified so make it random. if (E.Get().CurrTrial.trialData.StartPosition.Count == 0) { // Try to randomly place the character, checking for proximity // to the pickup location var i = 0; while (i++ < 100) { var CurrentTrialRadius = DS.GetData().Enclosures[E.Get().CurrTrial.TrialProgress.CurrentEnclosureIndex].Radius; var v = Random.insideUnitCircle * CurrentTrialRadius * 0.9f; var mag = Vector3.Distance(v, new Vector2(pickX, pickY)); if (mag > DS.GetData().CharacterData.DistancePickup) { transform.position = new Vector3(v.x, 0.5f, v.y); var camPos = Cam.transform.position; camPos.y = DS.GetData().CharacterData.Height; Cam.transform.position = camPos; return; } } Debug.LogError("Could not randomly place player. Probably due to" + " a pick up location setting"); } else { var p = E.Get().CurrTrial.trialData.StartPosition; if (useEnclosure) { p = new List <float>() { pickX, pickY }; } transform.position = new Vector3(p[0], 0.5f, p[1]); var camPos = Cam.transform.position; camPos.y = DS.GetData().CharacterData.Height; Cam.transform.position = camPos; } }
// Generates the landmarks, pretty similar to the data in pickup. private void GenerateLandmarks() { foreach (var p in E.Get().CurrTrial.trialData.LandMarks) { var d = DS.GetData().Landmarks[p - 1]; GameObject prefab; GameObject landmark; if (d.Type.ToLower().Equals("3d")) { prefab = (GameObject)Resources.Load("3D_Objects/" + d.Object, typeof(GameObject)); landmark = Instantiate(prefab); landmark.AddComponent <RotateBlock>(); landmark.GetComponent <Renderer>().material.color = Data.GetColour(d.Color); } else { // Load the "2D" prefab here, so we have the required components prefab = (GameObject)Resources.Load("3D_Objects/2D", typeof(GameObject)); landmark = Instantiate(prefab); var spriteName = d.Object; var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + spriteName); landmark.GetComponent <SpriteRenderer>().sprite = pic; } landmark.transform.localScale = d.ScaleVector; try { landmark.transform.position = d.PositionVector; } catch (Exception _) { landmark.transform.position = new Vector3(d.PositionVector.x, 0.5f, d.PositionVector.z); } //landmark.transform.Rotate(new Vector3(0, 1, 0), d.InitialRotation); landmark.transform.Rotate(d.RotationVector); landmark.GetComponent <Renderer>().material.color = Data.GetColour(d.Color); var sprite = d.ImageLoc; if (sprite != null) { var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + sprite); landmark.GetComponent <SpriteRenderer>().sprite = pic; } _created.Add(landmark); } }
private void GenerateWalls() { if ((int)E.Get().CurrTrial.enclosure.WallHeight == 0) { return; } //This computes the current interior angle of the given side. var interiorAngle = 360f / E.Get().CurrTrial.enclosure.Sides; //This is, of course, given as 360 / num sides //This sets the initial angle to the one given in the preset float currentAngle = 0; GameObject.Find("Ground").transform.localScale *= E.Get().CurrTrial.enclosure.Radius / 20f; GameObject.Find("Ground").transform.position = new Vector3(E.Get().CurrTrial.enclosure.Position[0], 0, E.Get().CurrTrial.enclosure.Position[1]); //Here we interate through all the sides for (var i = 0; i < E.Get().CurrTrial.enclosure.Sides; i++) { //We compute the sin and cos of the current angle (essentially plotting points on a circle var x = Cos(currentAngle) * E.Get().CurrTrial.enclosure.Radius + E.Get().CurrTrial.enclosure.Position[0]; var y = Sin(currentAngle) * E.Get().CurrTrial.enclosure.Radius + E.Get().CurrTrial.enclosure.Position[1]; //This is theoreticially the perfect length of the wall. However, this causes a multitude of problems //Such as: //Gaps appearing in large wall numbers //Desealing some stuff. so, bad. var length = 2 * E.Get().CurrTrial.enclosure.Radius *Tan(180f / E.Get().CurrTrial.enclosure.Sides); //Here we create the wall var obj = Instantiate(Wall, new Vector3(x, E.Get().CurrTrial.enclosure.WallHeight / 2 - .1f, y), Quaternion.identity ); // So we add 10 because the end user won't be able to notice it anyways obj.transform.localScale = new Vector3(length + 10, E.Get().CurrTrial.enclosure.WallHeight, 0.5f); // This rotates the walls by the current angle + 90 obj.transform.Rotate(Quaternion.Euler(0, -currentAngle - 90, 0).eulerAngles); // And we add the wall to the created list as to remove it later _created.Add(obj); // And of course we increment the interior angle. currentAngle += interiorAngle; } }
private void Start() { _created = new List <GameObject>(); GameObject.Find("CountDown").GetComponent <Text>().text = ""; var goalText = GameObject.Find("Goal").GetComponent <Text>(); goalText.GetComponent <RectTransform>().sizeDelta = new Vector2(300, 40); goalText.text = E.Get().CurrTrial.trialData.DisplayText ?? "Test"; goalText.color = Color.black; Generate2dWalls(); Generate2dLandmarks(); var previousTrial = E.Get().CurrTrial.TrialProgress.PreviousTrial; GameObject.Find("Plane").transform.localScale *= previousTrial.enclosure.Radius / 10f; }
private static Data.Point ReadFromExternal(string inputFile) { var scriptPath = DataSingleton.GetData().PythonScriptsPath + inputFile; var p = new Process { StartInfo = new ProcessStartInfo("python", "\"" + scriptPath + "\"") { RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, UseShellExecute = false, CreateNoWindow = true } }; p.Start(); p.StandardInput.Write(JsonUtility.ToJson(E.Get().CurrTrial.trialData) + "\n"); p.WaitForExit(); var line = p.StandardOutput.ReadLine(); while (!p.StandardError.EndOfStream) { var outputLine = p.StandardError.ReadLine(); UnityEngine.Debug.LogError(outputLine); } if (line == null) { UnityEngine.Debug.LogError("PYTHON FILE ERROR! (Most likely the wrong python version/environment)"); return(new Data.Point { X = 5, Y = 0, Z = 5 }); } var arr = line.Split(','); return(new Data.Point { X = float.Parse(arr[0]), Y = float.Parse(arr[2]), Z = float.Parse(arr[1]) }); }
private void Update() { E.LogData(TrialProgress.GetCurrTrial().TrialProgress, TrialProgress.GetCurrTrial().TrialStartTime, transform); // Wait for the sound to finish playing before ending the trial if (_playingSound) { if (!GetComponent <AudioSource>().isPlaying) { TrialProgress.GetCurrTrial().Progress(); _playingSound = false; } } // This first block is for the initial rotation of the character if (_currDelay < _waitTime) { doInitialRotation(); } else { // This section rotates the camera (potentiall up 15 degrees), basically deprecated code. if (!_reset) { Cam.transform.Rotate(0, 0, 0); _reset = true; TrialProgress.GetCurrTrial().ResetTime(); } // Move the character. try { ComputeMovement(); } catch (MissingComponentException e) { Debug.LogWarning("Skipping movement calc: instructional trial"); } } _currDelay += Time.deltaTime; }
// Generate walls based on the parameters of the previous enclosure // TODO: Make 2d trials work with their own enclosures. private void Generate2dWalls() { var previousTrial = E.Get().CurrTrial.TrialProgress.PreviousTrial; //This computes the current interior angle of the given side. var interiorAngle = 360f / previousTrial.enclosure.Sides; //This is, of course, given as 360 / num sides //This sets the initial angle to the one given in the preset float currentAngle = 0; //Here we interate through all the sides for (var i = 0; i < previousTrial.enclosure.Sides; i++) { //We compute the sin and cos of the current angle (essentially plotting points on a circle var x = GenerateWall.Cos(currentAngle) * previousTrial.enclosure.Radius + previousTrial.enclosure.Position[0]; var y = GenerateWall.Sin(currentAngle) * previousTrial.enclosure.Radius + previousTrial.enclosure.Position[1]; //This is theoreticially the perfect length of the wall. However, this causes a multitude of problems //Such as: //Gaps appearing in large wall numbers //Desealing some stuff. so, bad. var length = 2 * previousTrial.enclosure.Radius * GenerateWall.Tan(180f / previousTrial.enclosure.Sides); //Here we create the wall var obj = Instantiate(Wall, new Vector3(x, 0.001F, y), Quaternion.identity ); //So we add 10 because the end user won't be able to notice it anyways obj.transform.localScale = new Vector3(length, 4F, 0.5f); //This rotates the walls by the current angle + 90 obj.transform.Rotate(Quaternion.Euler(0, -currentAngle - 90, 0).eulerAngles); //And we add the wall to the created list as to remove it later _created.Add(obj); //And of course we increment the interior angle. currentAngle += interiorAngle; } }
// Here we setup the colours. This is done as a gradient utilizing data given from input.json private void SetupColours() { var col = Data.GetColour(E.Get().CurrTrial.enclosure.WallColor); MakeFloor = true; //And here we set the color of the wall prefab to the appropriate color if (E.Get().CurrTrial.enclosure.WallColor == "ffffff00") { Wall.GetComponent <Renderer>().enabled = false; GameObject.Find("Ground").GetComponent <Renderer>().enabled = false; E.Get().CurrTrial.enclosure.GroundTileSize = 0; MakeFloor = false; } if (E.Get().CurrTrial.enclosure.GroundColor == "ffffff00") { GameObject.Find("Ground").GetComponent <Renderer>().enabled = false; E.Get().CurrTrial.enclosure.GroundTileSize = 0; MakeFloor = false; } Wall.GetComponent <Renderer>().sharedMaterial.color = col; }
// Use this for initialization private void Start() { var gen = GameObject.Find("WallCreator").GetComponent <GenerateGenerateWall>(); _destroy = new List <GameObject>(); //This initializes the food object destroy list var activeGoals = E.Get().CurrTrial.trialData.ActiveGoals; var inactiveGoals = E.Get().CurrTrial.trialData.InactiveGoals; var invisibleGoals = E.Get().CurrTrial.trialData.InvisibleGoals; var inactiveSet = new HashSet <int>(inactiveGoals); var invisibleSet = new HashSet <int>(invisibleGoals); var activeSet = new HashSet <int>(activeGoals); var merged = new List <int>(); merged.AddRange(activeGoals); merged.AddRange(inactiveGoals); merged.AddRange(invisibleGoals); Data.Point p = new Data.Point { X = 0, Y = 0, Z = 0 }; foreach (var val in merged) { var goalItem = DS.GetData().Goals[Mathf.Abs(val) - 1]; UnityEngine.Debug.Log(goalItem); // Position is not set in the config file if (goalItem.Position.Count == 0) { p = ReadFromExternal(goalItem.PythonFile); } else { try { p = new Data.Point { X = goalItem.PositionVector.x, Y = goalItem.PositionVector.y, Z = goalItem.PositionVector.z }; } catch (Exception _) { p = new Data.Point { X = goalItem.PositionVector.x, Y = 0.5f, Z = goalItem.PositionVector.z }; } } GameObject prefab; GameObject obj; var spriteName = ""; if (goalItem.Type.ToLower().Equals("3d")) { prefab = (GameObject)Resources.Load("3D_Objects/" + goalItem.Object, typeof(GameObject)); obj = Instantiate(prefab); obj.AddComponent <RotateBlock>(); } else { // Load the "2D" prefab here, so we have the required components prefab = (GameObject)Resources.Load("3D_Objects/" + goalItem.Type.ToUpper(), typeof(GameObject)); obj = Instantiate(prefab); spriteName = goalItem.Object; } obj.transform.Rotate(goalItem.RotationVector); obj.transform.localScale = goalItem.ScaleVector; obj.transform.position = new Vector3(p.X, p.Y, p.Z); obj.AddComponent <PickupSound>(); obj.GetComponent <PickupSound>().Sound = Resources.Load <AudioClip>("Sounds/" + goalItem.Sound); if (!string.IsNullOrEmpty(spriteName)) { var pic = Img2Sprite.LoadNewSprite(DataSingleton.GetData().SpritesPath + spriteName); obj.GetComponent <SpriteRenderer>().sprite = pic; } var color = Data.GetColour(goalItem.Color); try { obj.GetComponent <Renderer>().material.color = color; obj.GetComponent <Renderer>().enabled = !invisibleSet.Contains(val); obj.GetComponent <Collider>().enabled = !inactiveSet.Contains(val); if (activeSet.Contains(val) || invisibleSet.Contains(val)) { obj.tag = "Pickup"; obj.GetComponent <Collider>().isTrigger = true; } } catch (Exception _) { print("Visibility not working"); } _destroy.Add(obj); } GameObject.Find("Participant").GetComponent <PlayerController>().ExternalStart(p.X, p.Z); }
private void Start() { try { var trialText = GameObject.Find("TrialText").GetComponent <Text>(); var blockText = GameObject.Find("BlockText").GetComponent <Text>(); var currBlockId = E.Get().CurrTrial.BlockID; // This section sets the text trialText.text = E.Get().CurrTrial.trialData.DisplayText; blockText.text = DS.GetData().Blocks[currBlockId].DisplayText; if (!string.IsNullOrEmpty(E.Get().CurrTrial.trialData.DisplayImage)) { var filePath = DS.GetData().SpritesPath + E.Get().CurrTrial.trialData.DisplayImage; var displayImage = GameObject.Find("DisplayImage").GetComponent <RawImage>(); displayImage.enabled = true; displayImage.texture = Img2Sprite.LoadTexture(filePath); } } catch (NullReferenceException e) { Debug.LogWarning("Goal object not set: running an instructional trial"); } Random.InitState(DateTime.Now.Millisecond); _currDelay = 0; // Choose a random starting angle if the value is not set in config if (E.Get().CurrTrial.trialData.StartFacing == -1) { _iniRotation = Random.Range(0, 360); } else { _iniRotation = E.Get().CurrTrial.trialData.StartFacing; } transform.Rotate(0, _iniRotation, 0); try { _controller = GetComponent <CharacterController>(); _gen = GameObject.Find("WallCreator").GetComponent <GenerateGenerateWall>(); Cam.transform.Rotate(0, 0, 0); } catch (NullReferenceException e) { Debug.LogWarning("Can't set controller object: running an instructional trial"); } _waitTime = E.Get().CurrTrial.trialData.Rotate; _reset = false; localQuota = E.Get().CurrTrial.trialData.Quota; // This has to happen here for output to be aligned properly TrialProgress.GetCurrTrial().TrialProgress.TrialNumber++; TrialProgress.GetCurrTrial().TrialProgress.Instructional = TrialProgress.GetCurrTrial().trialData.Instructional; TrialProgress.GetCurrTrial().TrialProgress.EnvironmentType = TrialProgress.GetCurrTrial().trialData.Scene; TrialProgress.GetCurrTrial().TrialProgress.CurrentEnclosureIndex = TrialProgress.GetCurrTrial().trialData.Enclosure - 1; TrialProgress.GetCurrTrial().TrialProgress.BlockID = TrialProgress.GetCurrTrial().BlockID; TrialProgress.GetCurrTrial().TrialProgress.TrialID = TrialProgress.GetCurrTrial().TrialID; TrialProgress.GetCurrTrial().TrialProgress.TwoDim = TrialProgress.GetCurrTrial().trialData.TwoDimensional; TrialProgress.GetCurrTrial().TrialProgress.LastX = TrialProgress.GetCurrTrial().TrialProgress.TargetX; TrialProgress.GetCurrTrial().TrialProgress.LastY = TrialProgress.GetCurrTrial().TrialProgress.TargetY; TrialProgress.GetCurrTrial().TrialProgress.TargetX = 0; TrialProgress.GetCurrTrial().TrialProgress.TargetY = 0; _isStarted = true; }
// Use this for initialization private void Start() { Create.transform.position = Vector3.zero; _currCreate = Instantiate(E.Get().CurrTrial.enclosure.Sides == 0 ? GenerateEnclosureFromFile : Create); }