How can I deal with Unity job system input and output? - c#

I am currently trying to create a script that randomly places objects such as trees, rocks, and bushes throughout my terrain (I'm using Sebastian Lague's procedural generated terrain in case your wondering.) It became quite performance intensive so I decided to try out Unity's job system. I followed a few basic tutorials and learned quite a bit, but I just can't figure out how to pass in input to the job, and then receive output. Unity keeps telling me that I can't have multiple threads change the values of the NativeArray I am sending the job. I don't really know how I would attempt to create a different NativeArray for each separate job, complete all the jobs, and then use the output from each job to complete the instantiation of the objects in the main thread.
Here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Jobs;
using Unity.Collections;
using Unity.Burst;
public class SpawnWorldObjects : MonoBehaviour
{
public WorldObject[] worldObjects;
NativeArray<bool> isTouchingOtherObject = new NativeArray<bool>(1, Allocator.TempJob);
NativeArray<Vector3> position = new NativeArray<Vector3>(1, Allocator.TempJob);
NativeArray<RaycastHit> hit = new NativeArray<RaycastHit>(1, Allocator.TempJob);
public void SpawnObjectsInChunk(Transform chunk, Vector2 sampleCenter, MeshSettings meshSettings, HeightMapSettings heightMapSettings)
{
NativeList<JobHandle> jobHandleList = new NativeList<JobHandle>(Allocator.Temp);
for (int i = 0; i < worldObjects.Length; i++)
{
for (int j = 0; j < worldObjects[i].numberOfObjectsPerChunk; j++)
{
HeightMap heightMap = HeightMapGenerator.GenerateHeightMap(meshSettings.numVertsPerLine, meshSettings.numVertsPerLine, heightMapSettings, meshSettings, sampleCenter);
CalculatePosition job = new CalculatePosition
{
sampleCenter = sampleCenter,
meshWorldSize = meshSettings.meshWorldSize,
meshScale = meshSettings.meshScale,
numVertsPerLine = meshSettings.numVertsPerLine,
minValue = heightMap.minValue,
maxValue = heightMap.maxValue,
worldObjectMinSpawnHeight = worldObjects[i].minSpawnHeight,
worldObjectMaxSpawnHeight = worldObjects[i].maxSpawnHeight,
worldObjectDistanceFromOtherObjects = worldObjects[i].distanceFromOtherObjects,
isTouchingOtherObject = isTouchingOtherObject,
position = position,
hit = hit,
};
jobHandleList.Add(job.Schedule());
}
}
JobHandle.CompleteAll(jobHandleList);
for (int i = 0; i < worldObjects.Length; i++)
{
for (int j = 0; j < worldObjects[i].numberOfObjectsPerChunk; j++)
{
if (!isTouchingOtherObject[0])
{
int typeOfObject = Random.Range(0, worldObjects[i].objectsToSpawn.Length - 1);
GameObject worldObject = Instantiate(worldObjects[i].objectsToSpawn[typeOfObject], new Vector3(position[0].x, hit[0].point.y, position[0].z), worldObjects[i].objectsToSpawn[typeOfObject].transform.rotation);
worldObject.transform.SetParent(chunk);
}
}
}
jobHandleList.Dispose();
isTouchingOtherObject.Dispose();
position.Dispose();
hit.Dispose();
}
}
[System.Serializable]
public class WorldObject
{
public GameObject[] objectsToSpawn;
public float distanceFromOtherObjects;
public int numberOfObjectsPerChunk;
[Range(0, 1)]
public float minSpawnHeight;
[Range(0, 1)]
public float maxSpawnHeight;
}
public struct CalculatePosition : IJob
{
public Vector2 sampleCenter;
public float meshWorldSize;
public float meshScale;
public int numVertsPerLine;
public float minValue;
public float maxValue;
public float worldObjectMinSpawnHeight;
public float worldObjectMaxSpawnHeight;
public float worldObjectDistanceFromOtherObjects;
public NativeArray<bool> isTouchingOtherObject;
public NativeArray<Vector3> position;
public NativeArray<RaycastHit> hit;
public void Execute()
{
float minSpawnHeight = Mathf.Lerp(minValue, maxValue, worldObjectMinSpawnHeight);
float maxSpawnHeight = Mathf.Lerp(maxValue, maxValue, worldObjectMaxSpawnHeight);
position[0] = new Vector3(Random.Range(sampleCenter.x - (float)meshWorldSize / 2, sampleCenter.x + (float)meshWorldSize / 2), 1000, Random.Range(sampleCenter.y - (float)meshWorldSize / 2, sampleCenter.y + (float)meshWorldSize / 2));
RaycastHit _hit;
if (Physics.Raycast(position[0], Vector3.down, out _hit, 10000, 1 << 7))
{
hit[0] = _hit;
if (_hit.point.y > minSpawnHeight && _hit.point.y < maxSpawnHeight)
{
RaycastHit[] hits = Physics.SphereCastAll(position[0], worldObjectDistanceFromOtherObjects, Vector3.down);
isTouchingOtherObject[0] = false;
foreach (RaycastHit __hit in hits)
{
if (__hit.collider.gameObject.layer == 9)
{
isTouchingOtherObject[0] = false;
return;
}
}
}
}
}
}

I figured it out. You have to make a list of every thing you want to do and use parallel jobs. Code Monkey on YouTube has some good content explaining this.

Related

Why all the speeds values are 0?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour
{
public GameObject prefabToRotate;
[Range(1, 100)]
public int numberOfObjects = 5;
[Range(1, 500)]
public float[] speeds;
public bool randomNumbersOfObjects = false;
public bool randomSpeed = false;
private List<GameObject> instantiatedObjects = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
speeds = new float[numberOfObjects];
if(randomNumbersOfObjects == true)
{
numberOfObjects = Random.Range(1, 100);
}
if(randomSpeed == true)
{
for(int i = 0; i < speeds.Length; i++)
{
speeds[i] = Random.Range(1, 500);
}
}
for(int i = 0; i < numberOfObjects; i++)
{
GameObject go = Instantiate(prefabToRotate);
instantiatedObjects.Add(go);
}
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < numberOfObjects; i++)
{
instantiatedObjects[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
}
}
}
And how can I get random numbers and random speeds from the Range sliders ? 1, 100 and 1, 500 ? I want also to be able to change this values of the sliders in the Update and it will update in real time while running the game the number of objects and the random speeds.
You set the length of your 'speeds' array to 'numberOfObjects', then you change the value of 'numberOfObjects', but your 'speeds' array still equals the old value of 'numberOfObjects'. Try setting the length of 'speeds' after you assign a random value to 'numberOfObjects', like so
if (randomNumbersOfObjects == true)
{
numberOfObjects = Random.Range(1, 100);
}
speeds = new float[numberOfObjects];

Check if two lines intersect

I need to check if two lines intersect. These are currently wrapped in edge colliders.
In my minimal example i using Collider2D.OverlapsCollider
public class EdgeColliderChecker : MonoBehaviour
{
public EdgeCollider2D e1;
public EdgeCollider2D e2;
void Update () {
Collider2D[] results1 = new Collider2D[1];
e1.OverlapCollider(new ContactFilter2D(), results1);
if (results1[0] != null)
{
Debug.Log(results1[0].name);
}
Collider2D[] results2 = new Collider2D[1];
e1.OverlapCollider(new ContactFilter2D(), results2);
if (results2[0] != null) {
Debug.Log(results2[0].name);
}
}
}
This is how i have set up my scene:
As you can see in the picture above the two lines clearly intersect.
The issue is that nothing is outputed to the console.
I am not 100% sure about how ContactFilter should be configured but looking at the documentation it is used for filtering out results. So leaving it blank should include everything.
I really only need to do the check between two lines. So a function that takes them as arguments and returns a bool indicating intersection would be most convenient. Unfortunetaly I could not find such function in Unity.
It should not be overly complicated to construct the function myself but i would prefer to use the functions unity provide as much as possible. So consider this more of a unity related question than a math related one.
EDIT:
Using Collider2D.IsTouching(Collider2D) does not seem to work either. I use the same setup as before with this code instead:
public class EdgeColliderChecker : MonoBehaviour
{
public EdgeCollider2D e1;
public EdgeCollider2D e2;
void Update () {
if (e1.IsTouching(e2)) {
Debug.Log("INTERSECTION");
}
}
}
Edit 2:
I tried creating my own method for this:
public static class EdgeColliderExtentions {
public static List<Collider2D> GetInterSections(this EdgeCollider2D collider)
{
List<Collider2D> intersections = new List<Collider2D>();
Vector2[] points = collider.points;
for (int i = 0; i < points.Length - 1; i++)
{
Vector2 curr = collider.transform.TransformPoint(points[i]);
Vector2 next = collider.transform.TransformPoint(points[i + 1]);
Vector2 diff = next - curr;
Vector2 dir = diff.normalized;
float distance = diff.magnitude;
RaycastHit2D[] results = new RaycastHit2D[30];
ContactFilter2D filter = new ContactFilter2D();
Debug.DrawLine(curr, curr + dir * distance, Color.red, 1 / 60f);
int hits = Physics2D.Raycast(curr, dir, filter, results, distance);
for (int j = 0; i < hits; i++)
{
Collider2D intersection = results[j].collider;
if (intersection != collider)
{
intersections.Add(intersection);
}
}
}
return intersections;
}
}
EdgeColliderChecker:
public class EdgeColliderChecker : MonoBehaviour
{
public EdgeCollider2D e1;
void Update ()
{
List<Collider2D> hits = e1.GetInterSections();
if (hits.Count > 0) {
Debug.Log(hits.Count);
}
}
}
Still nothing. Even though the points i calculate align perfectly with the collider:
I did the math for it and it seems to work ok, not tested very thorougly. The intersection check is a bit choppy if it is run while the colliders are moving around:
public class Line {
private Vector2 start;
private Vector2 end;
public Line(Vector2 start, Vector2 end)
{
this.start = start;
this.end = end;
}
public static Vector2 GetIntersectionPoint(Line a, Line b)
{
//y = kx + m;
//k = (y2 - y1) / (x2 - x1)
float kA = (a.end.y - a.start.y) / (a.end.x - a.start.x);
float kB = (b.end.y - b.start.y) / (b.end.x - b.start.x);
//m = y - k * x
float mA = a.start.y - kA * a.start.x;
float mB = b.start.y - kB * b.start.x;
float x = (mB - mA) / (kA - kB);
float y = kA * x + mA;
return new Vector2(x,y);
}
public static bool Intersects(Line a, Line b)
{
Vector2 intersect = GetIntersectionPoint(a, b);
if (Vector2.Distance(a.start, intersect) < Vector2.Distance(a.start, a.end) &&
Vector2.Distance(a.end, intersect) < Vector2.Distance(a.start, a.end))
{
return true;
}
return false;
}
}
public static class EdgeColliderExtentions
{
public static bool Intersects(this EdgeCollider2D collider, EdgeCollider2D other)
{
Vector2[] points = collider.points;
Vector2[] otherPoints = other.points;
for (int i = 0; i < points.Length - 1; i++)
{
Vector2 start = collider.transform.TransformPoint(points[i]);
Vector2 end = collider.transform.TransformPoint(points[i + 1]);
Line line = new Line(start, end);
for (int j = 0; j < otherPoints.Length - 1; j++)
{
Vector2 otherStart = other.transform.TransformPoint(otherPoints[i]);
Vector2 otherEnd = other.transform.TransformPoint(otherPoints[i + 1]);
Line otherLine = new Line(otherStart, otherEnd);
if (Line.Intersects(line, otherLine))
{
return true;
}
}
}
return false;
}
}
But I'd really like to use something provided by unity instead.
Use Collider.bounds.Intersects(Collider.bounds) to determine if two bounds are intersecting:
void Update () {
if (e1.bounds.Intersects(e2.bounds)) {
Debug.Log("Bounds intersecting");
}
}
This unfortunately won't let you know if the edges are intersecting. However, if this tests false, you can skip testing the edges.
I realized that i could exhange one of the edge colliders for a polygon collider for my use case.
Using a polygon and an edge collider with Collider2D.OverlapsCollider() works as expected.
I don't know if i should accept this as an answer or not because it does not solve the original question about finding a funciton in unity for line intersection.
You could get a relatively close approximation with the following:
You could create a script that adds several small box colliders evenly along the line, the more the better. And then just do normal collision detection. But the more boxes (higher precision), the more costly computation-wise.

How can I generate stairs?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateStairs : MonoBehaviour
{
public GameObject stairsPrefab;
public int delay = 3;
public int stairsNumber = 5;
public int stairsHeight = 0;
public Vector3 stairsPosition;
public Vector2 stairsSize;
// Use this for initialization
void Start ()
{
StartCoroutine(BuildStairs());
}
// Update is called once per frame
void Update ()
{
}
private IEnumerator BuildStairs()
{
for (float i = 0; i <= stairsSize.x; i++)
{
for (float k = 0; k <= stairsSize.y; k++)
{
stairsPosition = new Vector3(i, stairsHeight, k);
GameObject stairs = Instantiate(stairsPrefab, stairsPosition, Quaternion.identity);
stairs.transform.localScale = new Vector3(stairsSize.x, 1 , stairsSize.y);
stairsHeight += 1;
yield return new WaitForSeconds(delay);
}
}
}
private void CalculateNextStair()
{
}
}
I messed it up. For example I want to build 5 stairs but the loops are over the stairs size and not number of stairs.
Second it's creating 10 sets of stairs not 5 stairs:
Another problem is how can I make that each stair will be build slowly ? Now it's just Instantiate slowly with delay but how can I generate each stair with delay?
Screenshot of the script inspector:
My current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateStairs : MonoBehaviour
{
public GameObject stairsPrefab;
public float delay = 0.3f;
public int stairsNumber = 5;
public int stairsPositions = 0;
public int stairsStartPositionHeight = 0;
public float stairsScalingHaight = 1;
public Vector2 stairsPosition;
public Vector2 stairsSize;
// Use this for initialization
void Start()
{
StartCoroutine(BuildStairs());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator BuildStairs()
{
for (float i = 0; i <= stairsNumber; i++)
{
// x=0f, y=z=stairsHeight
stairsPosition = new Vector3(0f, stairsPositions, stairsPositions);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
stairs.transform.localScale = new Vector3(
stairsSize.x,
stairsScalingHaight,
stairsSize.y);
stairsStartPositionHeight += 1;
yield return new WaitForSeconds(delay);
}
}
private void CalculateNextStair()
{
}
}
There's no reason to loop over the size of the stairs at all; you want to loop over stairsNumber, which is yet unused in your code.
Also, you don't need to change the x component of your stairs' positions. Keep it at 0f (or whatever you need).
The y and z components of your stairs positions (relative to the starting point) should both be factors of stairHeight. In this particular case, you want them to be equal to stairHeight, so that you get "square" step shapes.
private IEnumerator BuildStairs()
{
for (int i = 0; i <= stairsNumber ; i++) {
// x=0f, y=z=stairsHeight
stairsPosition = new Vector3(0f, stairsHeight, stairsHeight);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
stairs.transform.localScale = new Vector3(
stairsSize.x,
1f ,
stairsSize.y);
stairsHeight += 1f;
yield return new WaitForSeconds(delay);
}
}
If you change stairSize to be a Vector3, then you can just use stairSize directly as the localScale, and increment stairsHeight by stairsSize.y instead of just 1f.
If you want to offset the starting position of your stairs, you need to include an offset. I recommend keeping it separate from the height counter until you need to add them.
Also, if you want to have rectangular sized steps, keep a widthFactor to multiply by the height to find how far each step moves horizontally.
Combining these changes might look like this:
Vector3 stairSize;
float stepWidthFactor=1f;
Vector3 stairsStartPosition;
private IEnumerator BuildStairs() {
for (int i = 0; i <= stairsNumber ; i++) {
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + stairsHeight,
stairsStartPosition.z + stairsHeight*stepWidthFactor);
GameObject stairs = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
stairsHeight += stairsSize.y;
stairs.transform.localScale = stairSize;
yield return new WaitForSeconds(delay);
}
}

How can I add/destroy new objects to existing formation?

In the manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FormationsManager : MonoBehaviour
{
public Transform squadMemeberPrefab;
public int numberOfSquadMembers = 20;
public int columns = 4;
public int gaps = 10;
public Formations formations;
private int numofmembers;
// Use this for initialization
void Start()
{
numofmembers = numberOfSquadMembers;
formations.Init(numberOfSquadMembers, columns, gaps);
GenerateSquad();
}
// Update is called once per frame
void Update()
{
if (numofmembers != numberOfSquadMembers)
{
GenerateSquad();
}
}
private void GenerateSquad()
{
Transform go = squadMemeberPrefab;
for (int i = 0; i < formations.newpositions.Count; i++)
{
go = Instantiate(squadMemeberPrefab);
go.position = formations.newpositions[i];
go.tag = "Squad Member";
go.transform.parent = gameObject.transform;
}
}
}
And the Formations script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Formations : MonoBehaviour
{
public List<Vector3> newpositions;
private int numberOfSquadMembers;
private int columns;
private int gaps;
private List<Quaternion> quaternions;
private Vector3 FormationSquarePositionCalculation(int index)
{
float posX = (index % columns) * gaps;
float posY = (index / columns) * gaps;
return new Vector3(posX, posY);
}
private void FormationSquare()
{
newpositions = new List<Vector3>();
quaternions = new List<Quaternion>();
for (int i = 0; i < numberOfSquadMembers; i++)
{
Vector3 pos = FormationSquarePositionCalculation(i);
Vector3 position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
newpositions.Add(position);
}
}
public void Init(int numberOfSquadMembers, int columns, int gaps)
{
this.numberOfSquadMembers = numberOfSquadMembers;
this.columns = columns;
this.gaps = gaps;
FormationSquare();
}
}
What I want to do is in the FormationsManager in the Update not only just calling GenerateSquad but to add the new once to the last/next position of the existing already formation.
void Update()
{
if (numofmembers != numberOfSquadMembers)
{
GenerateSquad();
}
}
If the value of numberOfSquadMembers is 20 first time and then I changed it to 21 add new object to the end of the formation and same if I change the value of numberOfSquadMembers for example from 20 to 19 or from 21 to 5 destroy the amount of objects from the end and keep the formation shape.
The soldiers the last line is on the right side.
So if I change the value to add more then add it to the right and if I change to less destroy from the right side. The most left line of soldiers is the first.
It is possible if you keep GameObject instances inside FormationsManager class, and then reuse them in GenerateSquad method.
In FormationsManager class, add and modify code as follows.
public GameObject squadMemeberPrefab;
List<GameObject> SquadMembers = new List<GameObject>();
void Update()
{
if (numofmembers != numberOfSquadMembers)
{
numofmembers = numberOfSquadMembers;
formations.Init(numberOfSquadMembers, columns, gaps);
GenerateSquad();
}
}
private void GenerateSquad()
{
Transform go = squadMemeberPrefab;
List<GameObject> newSquadMembers = new List<GameObject>();
int i = 0;
for (; i < formations.newpositions.Count; i++)
{
if (i < SquadMembers.Count)
go = SquadMembers[i];
else
{
go = Instantiate(squadMemeberPrefab);
newSquadMembers.Add(go);
}
go.position = formations.newpositions[i];
go.tag = "Squad Member";
go.transform.parent = gameObject.transform;
}
for (; i < SquadMembers.Count; i++)
Destroy(SquadMembers[i]);
SquadMembers = newSquadMembers;
}
However, I recommend you to consider GameObject Pool (Object Pool), which can thoroughly resolve such object recycle problem. For this purpose, you can use ClientScene.RegisterSpawnHandler. Go to this Unity Documentation page and search text "GameObject pool". You can see an example code there.

The name `Math' does not exist in the current context

I have the Code below and I'm trying to round the PerlinNoise(x,z) so I've put it equal to Yscale and tried to round it. the issue is that I get the error "The name `Math' does not exist in the current context" for that Line. Any Ideas?
using UnityEngine;
using System.Collections;
public class voxelcreate : MonoBehaviour {
private int origin = 0;
private Vector3 ChunkSize = new Vector3 (32,6,32);
private float Height = 10.0f;
private float NoiseSize = 10.0f;
private float Yscale=0;
private GameObject root;
public float PerlinNoise(float x, float y)
{
float noise = Mathf.PerlinNoise( x / NoiseSize, y / NoiseSize );
return noise * Height;
}
// Use this for initialization
void Start () {
for(int x = 0; x < 33; x++){
bool isMultiple = x % ChunkSize.x == 0;
if(isMultiple == true){
origin = x;
Chunk();}
}
}
// Update is called once per frame
void Chunk (){
int ranNumber = Random.Range(8, 80);
int ranNumber2 = Random.Range(8, 20);
Height = ranNumber2;
NoiseSize = ranNumber;
for(int x = 0; x < ChunkSize.x; x++)
{
for(int y = 0; y < ChunkSize.y; y++)
{
for(int z = 0; z < ChunkSize.z; z++)
{
GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
int Yscale = (int)Math.Round((PerlinNoise( x, z)), 0);
box.transform.position = new Vector3( origin+x , y+Yscale, z);
}}}}}
Add
using System;
at the top of the file.
Or use System.Math instead of Math.
It is true you could add using System. However, Unity has Mathf
Why would you rather use the built in Mathf?
System.Math uses doubles. UnityEngine.Mathf uses floats. Since most of Unity uses floats, it is better to use Mathf so that you don't have to constantly convert floats and doubles back and forth.
In ASP.NET Core, you need to get the System.Runtime.Extension package first, maybe via NuGet Package Manager in Visual Studio or by command line.
Details about the package can be found here.
Finally you need to give:
using System
And then you can use methods of this class:
using System;
namespace Demo.Helpers
{
public class MathDemo
{
public int GetAbsoluteValue()
{
var negativeValue = -123;
return Math.Abs(negativeValue);
}
}
}

Categories

Resources