lundi 7 mai 2018

How to create fixed and random sequences in C#?

I created a script that is able to spawn two different kinds of objects (A: Target / B: Distractors) on a variable number of circles. The target (a banana) is placed on a random position and every other position is filled with distractor objects (cheeseburgers). Currently I'm only collecting response time data on this endless repeatition of randomly placed targets. Here is a screenshot from the latest version to get you an idea. The script bellow does the job. To further advance the project I want to implement the following:

At the start of my experiment I want to randomly generate two fixed target locations (e.g. lower circle position 12, ...) that will be repeated and intermixed with the other trials. To give you an example the final result could look like this: (1) Create fixed location and begin experiment (2) fixed position (3) random positiob (4) random position (...) (16) fixed position (17) random position ...

 using UnityEngine;

public class Spawner : MonoBehaviour
{
// Researcher Input
public int numObjects = 36;
public float radius = 3.0f;
public float angle = 10.0f;
public int numCircles = 2;
public float spacingCircles = 2.0f;

public GameObject Distractor;
public GameObject Target;

private bool flipObject;
private Timer timer;

// Fancy feedback stuff
private AudioSource audioSource;
public AudioClip successSound;
public AudioClip failureSound;

// User input is handled once per frame
void Update ()
{
    HandleUserInput ();
}

// Load audio and timer at first frame
void Start ()
{
    audioSource = GetComponent<AudioSource> ();
    timer = GetComponent<Timer> ();

    // Create targets / distractors
    startTrial();
}

private void startTrial ()
{
    timer.StartRecord();
    Vector3 center = transform.position;

    int randomCircle = Random.Range(0, numCircles);
    flipObject = (Random.value > 0.5f);

    for (int i = 0; i < numCircles; i++)
    {
        DrawCircle(center, randomCircle == i);
        center.y += spacingCircles;
    }
}

void DrawCircle (Vector3 center, bool createRandomObject)
{
    int randomPosition = Random.Range (0, numObjects);
    for (int i = 0; i < numObjects; i++) {
        Vector3 pos = Circle (center, i * angle);
        Quaternion rot = Quaternion.LookRotation (pos - center);

        if (createRandomObject && i == randomPosition) {
            GameObject instanceOfObject2 = Instantiate (Target, pos, rot, transform);
            if (flipObject) {
                instanceOfObject2.transform.localScale = new Vector3 (-1.0f, 1.0f, 1.0f);
            }
            Debug.Log ("Banana position: " + pos.ToString ());

        } else {
            Instantiate (Distractor, pos, rot, transform);
        }
    }
}

Vector3 Circle (Vector3 center, float ang)
{
    Vector3 pos;
    pos.x = center.x + radius * Mathf.Sin (ang * Mathf.Deg2Rad);
    pos.z = center.z + radius * Mathf.Cos (ang * Mathf.Deg2Rad);
    pos.y = center.y;
    return pos;
}

void OnDrawGizmosSelected ()
{
    Gizmos.color = Color.green;
    Gizmos.DrawWireSphere (transform.position, radius);
}

private void DeleteAllChildren ()
{
    foreach (Transform child in transform) {
        GameObject.Destroy (child.gameObject);
    }
}
}




Aucun commentaire:

Enregistrer un commentaire