Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experiment Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ExperimentManager : MonoBehaviour {
[Header("Settings")]
public float distanceInterval;
public float xAxisIncrement;
public float zAxisIncrement;
public string logPath;
[Header("Setup Static")]
public GameObject firstPerson;
public GameObject secondPerson;
public GameObject thirdPerson;
public GameObject fourthPerson;
public Transform P1;
public Transform P2;
public Transform P2Position;
public Transform P3Position;
public Transform P4Position;
public LayerMask ignoreP1;
public LayerMask ignoreP1AndP2;
public LayerMask hitP4Layer;
public bool countPixels = false;
[Header("Dynamic Variables")]
public bool countPixelsForFrame = false;
public bool p2Overlap = false;
public bool p3Overlap = false;
public bool p4Overlap = false;
public bool movedThisFrame = false;
public bool p2IsDone = false;
public bool p3IsDone = false;
public bool p4IsDone = false;
public int blackPixels;
public int onePersonBlob;
public int twoPersonBlob;
public int threePersonBlob;
public int fourPersonBlob;
public int xPos;
public int yPos;
// Use this for initialization
void Start () {
secondPerson.transform.position = P2Position.transform.position;
thirdPerson.transform.position = P3Position.transform.position;
fourthPerson.transform.position = P4Position.transform.position;
//Write some text to the log file
StreamWriter writer = new StreamWriter(logPath, true);
//writer.WriteLine("Blob size at " + (firstPerson.transform.position.z - Camera.main.transform.position.z) + " metres from the camera, resolution of " + Camera.main.pixelWidth + "x" + Camera.main.pixelHeight);
// Debug.Log("distanceFromCamera blobPixels blobXPosition blobYPosition");
writer.WriteLine("distanceFromCamera onePersonBlob twoPersonBlob threePersonBlob fourPersonBlob blobXPosition blobYPosition");
writer.Close();
}
void OnGUI() {
// Show Screen Dimensions
GUI.Label(new Rect(10, 55, 100, 20), Camera.main.pixelWidth + " " + Camera.main.pixelHeight);
}
void MoveP2(){
if(!movedThisFrame){
secondPerson.transform.position = new Vector3(secondPerson.transform.position.x + xAxisIncrement,secondPerson.transform.position.y,secondPerson.transform.position.z);
movedThisFrame = true;
}
}
void MoveP3(){
if(!movedThisFrame){
thirdPerson.transform.position = new Vector3(thirdPerson.transform.position.x + xAxisIncrement,thirdPerson.transform.position.y,thirdPerson.transform.position.z);
movedThisFrame = true;
}
}
void MoveP4(){
if(!movedThisFrame){
fourthPerson.transform.position = new Vector3(fourthPerson.transform.position.x + xAxisIncrement,fourthPerson.transform.position.y,fourthPerson.transform.position.z);
movedThisFrame = true;
}
}
void ResetP2(){
secondPerson.transform.position = P2Position.position;
p2IsDone = false;
}
void ResetP3(){
thirdPerson.transform.position = P3Position.position;
p3IsDone = false;
}
void ResetP4(){
fourthPerson.transform.position = P4Position.position;
p4IsDone = false;
}
// Update is called once per frame
void Update () {
// Reset this to allow for a new movement
movedThisFrame = false;
// -----------------
// RAYCAST STUFF
// -----------------
RaycastHit hit;
Vector3 viewPos;
viewPos = Camera.main.WorldToScreenPoint(firstPerson.transform.position);
Ray ray = Camera.main.ScreenPointToRay(viewPos);
RaycastHit hit2;
Vector3 lineToP2;
lineToP2 = Camera.main.WorldToScreenPoint(secondPerson.transform.position);
Ray ray2 = Camera.main.ScreenPointToRay(lineToP2);
RaycastHit hit3;
Vector3 lineToP3;
lineToP3 = Camera.main.WorldToScreenPoint(thirdPerson.transform.position);
Ray ray4 = Camera.main.ScreenPointToRay(lineToP3);
// Ray from camera to players
Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow);
Debug.DrawRay(ray2.origin, ray2.direction * 100, Color.red);
Debug.DrawRay(ray4.origin, ray4.direction * 100, Color.green);
// -----------------
// CHECK COLLISIONS
// -----------------
// Check if P2 is behind P1
if(Physics.CapsuleCast(P1.position, P2.position, 0.125f, ray.direction, out hit, Mathf.Infinity, ignoreP1)){
if(hit.collider.gameObject.tag == "SecondPerson"){
p2Overlap = true;
}
}
// If the second player is not behind the first player
else{
p2Overlap = false;
}
// Check if P3 is behind P2
if(Physics.CapsuleCast(P1.position,P2.position,0.125f,ray2.direction,out hit2, Mathf.Infinity, ignoreP1AndP2)){
if(hit2.collider.gameObject.tag == "ThirdPerson"){
p3Overlap = true;
}
}
// If not
else{
p3Overlap = false;
}
// Check if P4 is behind P3
if(Physics.CapsuleCast(P1.position,P2.position,0.125f,ray4.direction,out hit3, Mathf.Infinity, hitP4Layer)){
if(hit3.collider.gameObject.tag == "FourthPerson"){
p4Overlap = true;
}
}
// If not
else{
p4Overlap = false;
}
// This checks if each person is on the right side of the person their overlap is checked with, and if said overlap has ended
if(secondPerson.transform.position.x > firstPerson.transform.position.x){
if(!p2Overlap){
p2IsDone = true;
}
}
if(thirdPerson.transform.position.x > secondPerson.transform.position.x){
if(!p3Overlap){
p3IsDone = true;
}
}
if(fourthPerson.transform.position.x > thirdPerson.transform.position.x){
if(!p4Overlap){
p4IsDone = true;
}
}
// -----------------
// MOVEMENT
// -----------------
// Left side movement
if(!p2Overlap){
if(!p2IsDone){
MoveP2();
}
}
if(!p3Overlap){
if(!p3IsDone){
MoveP3();
}
}
if(!p4Overlap){
if(!p4IsDone){
MoveP4();
}
}
// Movement Across
if(!p4IsDone){
if(p4Overlap){
MoveP4();
}
}
if(!p3IsDone){
if(p3Overlap){
MoveP3();
}
}
if(p4IsDone){
if(!p3IsDone){
if(p3Overlap){
MoveP3();
ResetP4();
}
}
else{
if(!p2IsDone){
MoveP2();
ResetP3();
ResetP4();
}
}
}
if(p2IsDone && p3IsDone && p4IsDone){
firstPerson.transform.position = new Vector3(firstPerson.transform.position.x,firstPerson.transform.position.y,firstPerson.transform.position.z + distanceInterval);
secondPerson.transform.position = P2Position.position;
thirdPerson.transform.position = P3Position.position;
fourthPerson.transform.position = P4Position.position;
p2IsDone = false;
p3IsDone = false;
p4IsDone = false;
}
// -----------------
// LOGGING
// -----------------
// Only log frames where everything overlaps
if(p2Overlap && p3Overlap && p4Overlap){
countPixelsForFrame = true;
}
// Pixel Counter and Logging Script
if(countPixels && countPixelsForFrame){
for(int x = 0; x < Camera.main.pixelWidth; x++){
for(int y = 0; y < Camera.main.pixelHeight; y++){
Ray ray3 = Camera.main.ScreenPointToRay(new Vector3(x, y, 0));
if(Physics.Raycast(ray3,out hit)){
if(hit.collider.gameObject.tag == "FirstPerson"){
onePersonBlob++;
if(y < yPos){
xPos = x;
yPos = y;
}
}
if(hit.collider.gameObject.tag == "SecondPerson"){
twoPersonBlob++;
if(y < yPos){
xPos = x;
yPos = y;
}
}
if(hit.collider.gameObject.tag == "ThirdPerson"){
threePersonBlob++;
if(y < yPos){
xPos = x;
yPos = y;
}
}
if(hit.collider.gameObject.tag == "FourthPerson"){
fourPersonBlob++;
if(y < yPos){
xPos = x;
yPos = y;
}
}
}
}
}
Debug.Log(firstPerson.transform.position.z - Camera.main.transform.position.z + " " + onePersonBlob + " " + (onePersonBlob+twoPersonBlob) + " " + (onePersonBlob+twoPersonBlob+threePersonBlob) + " " + (onePersonBlob+twoPersonBlob+threePersonBlob+fourPersonBlob)+ " " + xPos + " " + (480-yPos));
//Write some text to the log file
StreamWriter writer = new StreamWriter(logPath, true);
writer.WriteLine(firstPerson.transform.position.z - Camera.main.transform.position.z + " " + onePersonBlob + " " + (onePersonBlob+twoPersonBlob) + " " + (onePersonBlob+twoPersonBlob+threePersonBlob) + " " + (onePersonBlob+twoPersonBlob+threePersonBlob+fourPersonBlob)+ " " + xPos + " " + (480-yPos));
writer.Close();
xPos = 1000;
yPos = 1000;
blackPixels = 0;
onePersonBlob = 0;
twoPersonBlob = 0;
threePersonBlob = 0;
fourPersonBlob = 0;
// End Pixel Counter
}
countPixelsForFrame = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.