Ideen var at man med Hololensen kunne placere en serie af målskiver rundt om i et vilkårligt lokale og skyde dem med ildkugler.

Til dette skal der kunne spawne skydeskiver rundt omkring i et lokale, ved hjælp af Hololensens spatialing mapping, hvor den sender indfrarød lys ud, for at skabe en 3d skanning af et rum. Der til skal man sigte med ved at kigge på målet, og dertil affyrer en ildkugle mod målet, og ødelægge det, hvis det bliver ramt.

Dette skal til sammen give et spil, hvor man kan opleve mål 360 grader rund om spilleren, hvor problemer med motion sickness er løst med hjælp af brugen af AR.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class randomSpawn : MonoBehaviour {
    public GameObject target;                // The enemy prefab to be spawned.
    public float spawnTime = 3f;          // How long between each spawn.
    public float updateTime = 3f;         // How long between updates to _pts
    Camera cam;                           // Stores the active camera to be used for location purposes
    Vector3 camTransform;                 // Stores the camera's position once we know it.

    [Tooltip("Select the layers raycast should target.")]
    public LayerMask RaycastLayerMask = Physics.DefaultRaycastLayers; // Lets us define the layers to mask in the editor

    [Tooltip("Multiplies by this number to control the size of the sphere")]
    public float sizeReduction = 1;      // Controls the size of the sphere in the inspector

    private List<Vector3> _pts = new List<Vector3>();   //  A list to hold all the points found on the spher
    private List<SpawnRange> ranges = new List<SpawnRange>(); // A list to hold my struct with acceptable spawn ranges
                                                              // Use this for initialization

public struct SpawnRange            // A Struct is born to hold the starting and end points from my raycast
    {
        public Vector3 origin;
        public Vector3 endpoint;
    }

void Start () {

        InvokeRepeating("UpdateSpawnRanges", 0, updateTime);    // Calls the update function immeditately at start and updates per updateTime
        InvokeRepeating("Spawn", spawnTime, spawnTime);         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.

        cam = (Camera)GameObject.Find("Main Camera").GetComponent(typeof(Camera)); // Finds the camera component of our Main Camera

        CreatePoints(); // Creates the points that we will use throughout our script
    }

void CreatePoints()
    {
        float N = 50; // Numbers of points to spawn
        float y; // Used to calculate y
        float r; // Used to calculate the radius
        float phi; // Used to calculate the golden number

        float inc = Mathf.PI * (3 - Mathf.Sqrt(5));
        float off = 2 / N;

        for (var k = 0; k < N; k++)
        {
            y = k * off - 1 + (off / 3);
            r = Mathf.Sqrt(1 - y * y);
            phi = k * inc;
            _pts.Add(new Vector3(Mathf.Cos(phi) * r, y, Mathf.Sin(phi) * r));
        }
    }

void UpdateSpawnRanges()
    {
        Vector3 direction;        // Stores the direction our rays should go

        ranges.Clear();      // Clears the existing list of ranges before adding the new ones

        camTransform = cam.transform.position - new Vector3(0, 0, 0.5f);  // Lowers the location of our raycasting sphere in the world

        foreach (var pt in _pts)        // Every point in our list
        {
            RaycastHit hitInfo;    // Stores our raycast's hit

            direction = (pt * (sizeReduction + 0.75f) - (pt * sizeReduction)).normalized;     // Creates a big and a small sphere based on the points generated, and calculates the direction


            Physics.SphereCast((pt * sizeReduction) + camTransform, 4f, direction, out hitInfo, 20f, Physics.DefaultRaycastLayers);     // Does the actual spherecast

            SpawnRange spawnRange;      // Creates our struct to store the range
            spawnRange.origin = (pt * sizeReduction) + camTransform;    // Uses the location of our small sphere as the first point
            spawnRange.endpoint = hitInfo.point;        // Uses the location that our spherecast hit to determine the end point
            ranges.Add(spawnRange);     // Adds the above range to one big list
        }
    }

void Spawn()
    {
        float randomX = 3f;                 // Used to hold my random X
        float randomY = 3f;                 // Used to hold my random Y
        float randomZ = 3f;                 // Used to hold my random Z

        // Picks a random point range from our list of ranges
        SpawnRange randomRange = ranges[Random.Range(0, ranges.Count)];

        // Sets the varibales to randomize the location.
        randomX = Random.Range(randomRange.origin.x, randomRange.endpoint.x);
        randomY = Random.Range(randomRange.origin.y, randomRange.endpoint.y);
        randomZ = Random.Range(randomRange.origin.z, randomRange.endpoint.z);


        // Create an instance of the prefab at the randomly selected spawn point's position and rotation.
        Instantiate(target, new Vector3(randomX, randomY, randomZ), Quaternion.identity);
        Debug.Log(Quaternion.identity);
    }

}

 

Leave a Reply