How to draw multiple circles by using "LineRenderer" - unity3d

There is a code for a drawing circle with LineRenderer.
but I want to draw multiple circles with different radius, I used "for loop" but there is one circle instead of multiple
public float ThetaScale = 0.01f;
public float radius = 3f;
private int Size;
private LineRenderer LineDrawer;
private float Theta = 0f;
void Start ()
{
LineDrawer = GetComponent<LineRenderer>();
}
void Update ()
{
Theta = 0f;
Size = (int)((1f / ThetaScale) + 1f);
LineDrawer.SetVertexCount(Size);
for (int l = 0; l < 5; l++)
{
for(int i = 0; i < Size; i++)
{
Theta += (2.0f * Mathf.PI * ThetaScale);
float x = l * radius * Mathf.Cos(Theta);
float y = l * radius * Mathf.Sin(Theta);
LineDrawer.SetPosition(i, new Vector3(x, 0, y));
}
}
}

In every loop you always overwrite the same positions indices in the same line renderer. So you will always only have the last circle.
Note that it is also quite expensive to use SetPoisition repeatedly. As it says in the API you should rather work on an array and then use SetPoisitions to assign all positions at once.
One thing is a bit unclear though: If you use one single LineRenderer you won't get independent circles but they will always be connected at some point. Otherwise you would need 5 separated LineRenderer instances.
Option A: 5 circles but connected to each other since part of a single LineRenderer
void Start ()
{
LineDrawer = GetComponent<LineRenderer>();
LineDrawer.loop = false;
Theta = 0f;
// Use one position more to close the circle
Size = (int)((1f / ThetaScale) + 1f) + 1;
LineDrawer.positionCount = 5 * Size;
var positions = new Vector3[5 * Size];
for (int l = 0; l < 5; l++)
{
for(int i = 0; i < Size; i++)
{
Theta += (2.0f * Mathf.PI * ThetaScale);
float x = l * radius * Mathf.Cos(Theta);
float y = l * radius * Mathf.Sin(Theta);
positions[5 * l + i] = new Vector3(x, 0, y);
}
}
LineDrawer.SetPositions(positions);
}
Option B: 5 separated circles in 5 separated LineRenderers
// Drag 5 individual LineRenderer here via the Inspector
public LineRenderer[] lines = new LineRenderer[5];
void Start ()
{
foreach(var line in lines)
{
line.loop = true;
Theta = 0f;
Size = (int)((1f / ThetaScale) + 1f);
line.positionCount = Size;
var positions = new Vector3[Size];
for(int i = 0; i < Size; i++)
{
Theta += (2.0f * Mathf.PI * ThetaScale);
float x = l * radius * Mathf.Cos(Theta);
float y = l * radius * Mathf.Sin(Theta);
positions[5 * l + i] = new Vector3(x, 0, y);
}
line.SetPositions(positions);
}
}

You missed few details here and there. Here, this will work:
using UnityEngine;
[ExecuteAlways]
[RequireComponent( typeof(LineRenderer) )]
public class CircularBehaviour : MonoBehaviour
{
[SerializeField][Min(3)] int _numSegments = 16;
[SerializeField][Min(1)] int _numCircles = 5;
[SerializeField] float _radius = 3f;
LineRenderer _lineRenderer;
void Awake ()
{
_lineRenderer = GetComponent<LineRenderer>();
_lineRenderer.loop = false;
_lineRenderer.useWorldSpace = false;
}
void Update ()
{
const float TAU = 2f * Mathf.PI;
float theta = TAU / (float)_numSegments;
int numVertices = _numSegments + 1;
_lineRenderer.positionCount = numVertices * _numCircles;
int vert = 0;
for( int l=1 ; l<=_numCircles ; l++ )
{
float r = _radius * (float)l;
for( int i=0 ; i<numVertices ; i++ )
{
float f = theta * (float)i;
Vector3 v = new Vector3{ x=Mathf.Cos(f) , y=Mathf.Sin(f) } * r;
_lineRenderer.SetPosition( vert++ , v );
}
}
}
}
But
as #derHugo explained, this is not what you're looking for exactly as all circles will be drawn connected.

Related

Unity loop Application.EnterPlayMode

When I hit Play Unity makes me wait infinite time before start the scene. The message in Hold On window is "Application.EnterPlayMode Waiting for Unity's code to finish executing". There is only one scene in URP with global volume, directional light and a plane with this script:
using UnityEngine;
public class PerlinNoise : MonoBehaviour
{
[Header("Resolution")]
[Space]
public int width = 256;
public int heigth = 256;
[Space]
[Header("Adjustments")]
[Space]
public float scale = 20;
public float xOffset = 10;
public float yOffset = 10;
void Update()
{
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = GenerateTexture();
}
Texture2D GenerateTexture()
{
Texture2D texture = new Texture2D(width, heigth);
for (int x = 0; x < width; x++)
{
for (int y = 0; x < heigth; y++)
{
Color color = GenerateColor(x, y);
texture.SetPixel(width, heigth, color);
}
}
texture.Apply();
return texture;
}
Color GenerateColor(int x, int y)
{
float xCoord = (float)x / width * scale + xOffset;
float yCoord = (float)y / width * scale + yOffset;
float perlinNoise = Mathf.PerlinNoise(xCoord, yCoord);
return new Color(perlinNoise, perlinNoise, perlinNoise);
}
}
I tried to kill unity editor task in task manager and restart unity but the same issue repeats. Please help me
You have an infinite loop inside your GenerateTexture method. Specifically, the condition in the nested loop (for y) is accidentally checking for x:
for (int x = 0; x < width; x++)
{
for (int y = 0; x < heigth; y++) // x < height SHOULD BE y < height
{
Color color = GenerateColor(x, y);
texture.SetPixel(width, heigth, color); // this should probably be (x, y, color)
}
}

How to implement Offset on Perlin noise?

I need a little help,
I have this Perlin noise function, but I don't know how to properly create offsets.
I am using this to create infinite terrain generation and when I use this script it the noise values of individual chunks don't fit together properly. And they create holes.
Is there a way of fixing this ?
public float[,] GenerateNoise(int chunkSize, int octaves, string seed, float noiseScale, float persistence, float lacunarity, Vector2 offset)
{
if (noiseScale <= 0)
{
noiseScale = 0.0001f;
}
float halfWidth = chunkSize / 2f;
float halfHeight = chunkSize / 2f;
float[,] noiseMap = new float[chunkSize, chunkSize];
System.Random rand = new System.Random(seed.GetHashCode());
//Octaves offset
Vector2[] octavesOffset = new Vector2[octaves];
for (int i = 0; i < octaves; i++)
{
float offset_X = rand.Next(-100000, 100000) + offset.x;
float offset_Y = rand.Next(-100000, 100000) + offset.y;
octavesOffset[i] = new Vector2(offset_X / chunkSize , offset_Y / chunkSize);
}
for (int x = 0; x < chunkSize; x++)
{
for (int y = 0; y < chunkSize; y++)
{
float amplitude = 1;
float frequency = 1;
float noiseHeight = 0;
float superpositionCompensation = 0;
for (int i = 0; i < octaves; i++)
{
float sampleX = (x - halfWidth) / noiseScale * frequency + octavesOffset[i].x * frequency;
float sampleY = (y - halfHeight) / noiseScale * frequency + octavesOffset[i].y * frequency;
float noiseValue = Mathf.PerlinNoise(sampleX, sampleY);
noiseHeight += noiseValue * amplitude;
noiseHeight -= superpositionCompensation;
amplitude *= persistence;
frequency *= lacunarity;
superpositionCompensation = amplitude / 2;
}
noiseMap[x, y] = Mathf.Clamp01(noiseHeight);
}
}
return noiseMap;
}
It is quite simple actually, just add the chunk x,y coordinates to Mathf.PerlinNoise. Taking your code as an example, you can:
Pass chunkPosition as an argument to it:
public float[,] GenerateNoise(Vector2 chunkPos, int chunkSize, int octaves, string seed, float noiseScale, float persistence, float lacunarity, Vector2 offset)
Add it to Mathf.PerlinNoise invocation:
float noiseValue = Mathf.PerlinNoise(sampleX + chunkPos.x, sampleY + chunkPos.y);
Then make sure to generate each chunk with an appropriate chunkPos, where chunkPos can be its transform.position or whatever coordinates you have.
That's it.

Unity perlin noise having repeating patterns

I made a Noise class using the Perlin Noise from Unity like this:
public static float[,] GetNoise(Vector2Int initialOffset, float scale, float persistance, float lacunarity, int octaves)
{
float[,] noiseMap = new float[Chunk.width, Chunk.height];
float maxHeight = 0;
float minHeight = 0;
for (int y = 0; y < Chunk.height; y++)
{
for (int x = 0; x < Chunk.width; x++)
{
float amplitude = 1;
float frequency = 1;
float noiseHeight = 0;
for (int oc = 0; oc < octaves; oc++)
{
float coordX = (x + initialOffset.x) / scale * frequency;
float coordY = (y + initialOffset.y) / scale * frequency;
float perlin = Mathf.PerlinNoise(coordX, coordY) * 2 - 1;
noiseHeight += perlin * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
if (noiseHeight < minHeight)
{
minHeight = noiseHeight;
}
if (noiseHeight > maxHeight)
{
maxHeight = noiseHeight;
}
noiseMap[x, y] = noiseHeight;
}
}
for (int y = 0; y < Chunk.height; y++)
{
for (int x = 0; x < Chunk.width; x++)
{
noiseMap[x, y] = Mathf.InverseLerp(minHeight, maxHeight, noiseMap[x, y]);
}
}
return noiseMap;
}
However this code is giving me repeating patterns like this:
What am I doing wrong? Or there is no way to get rid of the patterns?
I got it working, not very well, but working. The way I did was I generate the height map for every tile in the chunk, then I did some random placing of tiles, while having in account the height map. Something like this:
if (heightMap[x, y] < 0.3 && Random.value < 0.5)
// Add tile
This way I got this result:
EDIT:
Doing some more research about Perlin Noise I found out that it just doesn't like negative coords for some reason, so I did this way, hope this helps someone!
so .. fixed the negative coords like this:
//account for negatives (ex. -1 % 256 = -1, needs to loop around to 255)
if (noiseOffset.x < 0)
noiseOffset = new Vector2(noiseOffset.x + noiseRange.x, noiseOffset.y);
if (noiseOffset.y < 0)
noiseOffset = new Vector2(noiseOffset.x, noiseOffset.y + noiseRange.y);

Rotate a position according to forward

I am currently making a climbing detection system that follows the player around and detects the closest point the player can climb. I have a game object that casts rays according to the players position. The rays move with the player but do not rotate around. How can I make them rotate with the player ?
This is the code that draws the rays
void Update()
{
//Debug.DrawRay();
raySpace = surfaceWidth / definition;
raySpaceDividedByTwo = raySpace / 2;
surfaceWidthDividedByTwo = surfaceWidth / 2;
for (int i = 0; i < definition; i++)
{
for (int y = 0; y < definition; y++)
{
raycastCoordinates = new Vector3(
(this.transform.position.x + (i * raySpace) + raySpaceDividedByTwo) - surfaceWidthDividedByTwo,
this.transform.position.y,
(this.transform.position.z + (y * raySpace) + raySpaceDividedByTwo) - surfaceWidthDividedByTwo);
//offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
//raycastCoordinates = Quaternion.AngleAxis(this.transform.rotation.y, Vector3.up) * raycastCoordinates;
raycastUpHitsList.Add(Physics.RaycastAll(raycastCoordinates, Vector3.up, detectionHeight, LayerMask.GetMask("Water")));
Debug.DrawRay(raycastCoordinates, Vector3.up * detectionHeight, Color.green, 0.1f);
}
}
}
This should work:
void Update()
{
//Debug.DrawRay();
raySpace = surfaceWidth / definition;
raySpaceDividedByTwo = raySpace / 2;
surfaceWidthDividedByTwo = surfaceWidth / 2;
for (int i = 0; i < definition; i++)
{
for (int y = 0; y < definition; y++)
{
raycastCoordinates = new Vector3(
(this.transform.position.x + (i * raySpace) + raySpaceDividedByTwo) - surfaceWidthDividedByTwo,
this.transform.position.y,
(this.transform.position.z + (y * raySpace) + raySpaceDividedByTwo) - surfaceWidthDividedByTwo) * transform.forward;//if this doesn't work, try transform.right instead
//offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
//raycastCoordinates = Quaternion.AngleAxis(this.transform.rotation.y, Vector3.up) * raycastCoordinates;
raycastUpHitsList.Add(Physics.RaycastAll(raycastCoordinates, Vector3.up, detectionHeight, LayerMask.GetMask("Water")));
Debug.DrawRay(raycastCoordinates, Vector3.up * detectionHeight, Color.green, 0.1f);
}
}
}

CatmullRomSpline does not accept type arguement

I am looking to create smooth paths for my 2D game. Looking at CatmullRomSpline it is just the thing i need. Every post, even here on SE is giving it a type and passing all the control points and a Boolean with the constructor. This seems to be obsolete now, CatmullRomSpline does not accept any type parameters anymore and without it it can only work with V3 paths. Neither does the constructor accept a list of control points.
cp = new Vector2[]
{
new Vector2(0,100), new Vector2(100,600), new Vector2(300,300), new Vector2(600, 400)
};
CatmullRomSpline<Vector2> path = new CatmullRomSpline<Vector2>(cp, true);
This gives the following error: The type CatmullRomSpline is not generic; it cannot be parameterized with arguments <Vector2>.
Am i missing something or does CatmullRomSpline work differently nowadays, and how?
This is the CatmullRomSpline Class from badlogic. It surely looks like things changed, i am getting this class from "import com.badlogic.gdx.math.CatmullRomSpline;"
public class CatmullRomSpline implements Serializable { private
static final long serialVersionUID = -3290464799289771451L; private
List controlPoints = new ArrayList(); Vector3 T1 =
new Vector3(); Vector3 T2 = new Vector3();
/** Adds a new control point * * #param point the point */
public void add (Vector3 point) { controlPoints.add(point); }
/** #return all control points */ public List
getControlPoints () { return controlPoints; }
/** Returns a path, between every two control points numPoints are
generated and the control points themselves are added too. * The
first and the last controlpoint are omitted. if there's less than 4
controlpoints an empty path is returned. * * #param numPoints
number of points returned for a segment * #return the path */
public List getPath (int numPoints) { ArrayList
points = new ArrayList();
if (controlPoints.size() < 4) return points;
Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3();
for (int i = 1; i <= controlPoints.size() - 3; i++) {
points.add(controlPoints.get(i)); float increment = 1.0f /
(numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i +
2)).sub(controlPoints.get(i)).mul(0.5f);
for (int j = 0; j < numPoints; j++) {
float h1 = 2 * t * t * t - 3 * t * t + 1; // calculate basis
// function 1
float h2 = -2 * t * t * t + 3 * t * t; // calculate basis
// function 2
float h3 = t * t * t - 2 * t * t + t; // calculate basis
// function 3
float h4 = t * t * t - t * t; // calculate basis function 4
Vector3 point = new Vector3(controlPoints.get(i)).mul(h1);
point.add(controlPoints.get(i + 1).tmp().mul(h2));
point.add(T1.tmp().mul(h3));
point.add(T2.tmp().mul(h4));
points.add(point);
t += increment; } }
if (controlPoints.size() >= 4)
points.add(controlPoints.get(controlPoints.size() - 2));
return points; }
/** Returns a path, between every two control points numPoints are
generated and the control points themselves are added too. * The
first and the last controlpoint are omitted. if there's less than 4
controlpoints an empty path is returned. * * #param points the
array of Vector3 instances to store the path in * #param numPoints
number of points returned for a segment */ public void getPath
(Vector3[] points, int numPoints) { int idx = 0; if
(controlPoints.size() < 4) return;
for (int i = 1; i <= controlPoints.size() - 3; i++) {
points[idx++].set(controlPoints.get(i)); float increment = 1.0f
/ (numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i +
2)).sub(controlPoints.get(i)).mul(0.5f);
for (int j = 0; j < numPoints; j++) {
float h1 = 2 * t * t * t - 3 * t * t + 1; // calculate basis
// function 1
float h2 = -2 * t * t * t + 3 * t * t; // calculate basis
// function 2
float h3 = t * t * t - 2 * t * t + t; // calculate basis
// function 3
float h4 = t * t * t - t * t; // calculate basis function 4
Vector3 point = points[idx++].set(controlPoints.get(i)).mul(h1);
point.add(controlPoints.get(i + 1).tmp().mul(h2));
point.add(T1.tmp().mul(h3));
point.add(T2.tmp().mul(h4));
t += increment; } }
points[idx].set(controlPoints.get(controlPoints.size() - 2)); }
/** Returns all tangents for the points in a path. Same semantics as
getPath. * * #param numPoints number of points returned for a
segment * #return the tangents of the points in the path */ public
List getTangents (int numPoints) { ArrayList
tangents = new ArrayList();
if (controlPoints.size() < 4) return tangents;
Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3();
for (int i = 1; i <= controlPoints.size() - 3; i++) { float
increment = 1.0f / (numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i +
2)).sub(controlPoints.get(i)).mul(0.5f);
tangents.add(new Vector3(T1).nor());
for (int j = 0; j < numPoints; j++) {
float h1 = 6 * t * t - 6 * t; // calculate basis function 1
float h2 = -6 * t * t + 6 * t; // calculate basis function 2
float h3 = 3 * t * t - 4 * t + 1; // calculate basis function 3
float h4 = 3 * t * t - 2 * t; // calculate basis function 4
Vector3 point = new Vector3(controlPoints.get(i)).mul(h1);
point.add(controlPoints.get(i + 1).tmp().mul(h2));
point.add(T1.tmp().mul(h3));
point.add(T2.tmp().mul(h4));
tangents.add(point.nor());
t += increment; } }
if (controlPoints.size() >= 4)
tangents.add(T1.set(controlPoints.get(controlPoints.size() -
1)).sub(controlPoints.get(controlPoints.size() - 3))
.mul(0.5f).cpy().nor());
return tangents; }
/** Returns all tangent's normals in 2D space for the points in a
path. The controlpoints have to lie in the x/y plane for this * to
work. Same semantics as getPath. * * #param numPoints number of
points returned for a segment * #return the tangents of the points
in the path */ public List getTangentNormals2D (int
numPoints) { ArrayList tangents = new ArrayList();
if (controlPoints.size() < 4) return tangents;
Vector3 T1 = new Vector3(); Vector3 T2 = new Vector3();
for (int i = 1; i <= controlPoints.size() - 3; i++) { float
increment = 1.0f / (numPoints + 1); float t = increment;
T1.set(controlPoints.get(i + 1)).sub(controlPoints.get(i -
1)).mul(0.5f); T2.set(controlPoints.get(i +
2)).sub(controlPoints.get(i)).mul(0.5f);
Vector3 normal = new Vector3(T1).nor(); float x = normal.x;
normal.x = normal.y; normal.y = -x; tangents.add(normal);
for (int j = 0; j < numPoints; j++) {
float h1 = 6 * t * t - 6 * t; // calculate basis function 1
float h2 = -6 * t * t + 6 * t; // calculate basis function 2
float h3 = 3 * t * t - 4 * t + 1; // calculate basis function 3
float h4 = 3 * t * t - 2 * t; // calculate basis function 4
Vector3 point = new Vector3(controlPoints.get(i)).mul(h1);
point.add(controlPoints.get(i + 1).tmp().mul(h2));
point.add(T1.tmp().mul(h3));
point.add(T2.tmp().mul(h4));
point.nor();
x = point.x;
point.x = point.y;
point.y = -x;
tangents.add(point);
t += increment; } }
return tangents; }
/** Returns the tangent's normals using the tangent and provided up
vector doing a cross product. * * #param numPoints number of
points per segment * #param up up vector * #return a list of
tangent normals */ public List getTangentNormals (int
numPoints, Vector3 up) { List tangents =
getTangents(numPoints); ArrayList normals = new
ArrayList();
for (Vector3 tangent : tangents) normals.add(new
Vector3(tangent).crs(up).nor());
return normals; }
public List getTangentNormals (int numPoints, List
up) { List tangents = getTangents(numPoints);
ArrayList normals = new ArrayList();
int i = 0; for (Vector3 tangent : tangents) normals.add(new
Vector3(tangent).crs(up.get(i++)).nor());
return normals; } }
Your code should work fine according to the api and the source.
The class IS generic. You must be using some old version of the class.
Update to the latest version and the error should be solved.
Hope this helps.