Cannot Connect Path Points with Line Renderer in Unity - c#

I am trying to generate a pathway system where a line is being rendered with time. My code is:
public IEnumerator DrawPath(List<Vector3> pathWay)
{
lineRenderer.positionCount = 0;
points = new Vector3[pathWay.Count];
for (int i = 0; i < pathWay.Count; i++)
{
Vector3 checkpointpos = pathWay[i];
points[i] = new Vector3(checkpointpos.x, checkpointpos.y, checkpointpos.z);
}
lineRenderer.startWidth = 1f;
lineRenderer.endWidth = 1f;
lineRenderer.positionCount = points.Length;
for (int i = 0; i < points.Length; i++)
{
//Mathf.Lerp
yield return new WaitForSeconds(0.1f);
lineRenderer.SetPosition(i, points[i]);
}
}
Output:
I am getting List<Vector3> from A* Pathfinding free version plugin.
Any kind of help would be appreciated. Thanks in advance.

Okay I resolved the issue.
The problem was setting the counts of lines all at once so whenever I was setting a new vector it starts from position 0 that's why I had problem.
Solution:
public IEnumerator DrawPath(List<Vector3> pathWay)
{
lineRenderer.positionCount = 0;
points = new Vector3[pathWay.Count];
lineRenderer.startWidth = 1f;
lineRenderer.endWidth = 1f;
for (int i = 0; i < pathWay.Count; i++)
{
Vector3 checkpointpos = pathWay[i];
points[i] = new Vector3(checkpointpos.x, checkpointpos.y, checkpointpos.z);
yield return new WaitForSeconds(0.1f);
lineRenderer.positionCount++;
lineRenderer.SetPosition(i, points[i]);
}
}
Output:

Related

is there a way to fix this so it doesnt destroy the body

The code is a snippet of code from the torso but if you press 'f' on the torso it fires a laser. If the characters arm moves in front of the laser while it is firing it will get cut off, is there a way to fix this? PS: I'm not that good at coding. :P
laser.enabled = true;
for (int i = 0; i < iterations; i++)
{
var end = Physics2D.Raycast(BarrelPosition, BarrelDirection, 10000, bounds);
var endPos = end.transform == null ? (Vector3)BarrelPosition + (Vector3)BarrelDirection * 10000 : (Vector3)end.point;
var victimCount = Physics2D.LinecastNonAlloc(BarrelPosition, endPos, victimBuffer, layers);
laser.SetPosition(1, components.transform.InverseTransformPoint(endPos));
for (int n = 0; n < victimCount; n++)
{
var victim = victimBuffer[n];
victim.transform.SendMessage("Slice", SendMessageOptions.DontRequireReceiver);
// phys.rigidbody.AddForceAtPosition(forward * DirectionalForce * phys.rigidbody.mass, hit.point, ForceMode2D.Impulse);
}
CameraShakeBehaviour.main.Shake(1, transform.position);
physicalBehaviour.rigidbody.AddRelativeForce(new Vector2(0f * transform.localScale.normalized.x, 0), ForceMode2D.Impulse);
yield return new WaitForSeconds(interval);
}
laser.enabled = false;
yield return new WaitForSeconds(2f);
}
public override Vector2 BarrelPosition { get { return transform.TransformPoint(barrelPosition); } }
public Vector2 BarrelDirection { get { return transform.TransformDirection(barrelDirection) * transform.localScale.x; } }

I need to apply a Vector3 to a Vector3[] with Vector3.up, Vector3.down, ecc.but I can't

I have this script and I need to assign a different value to vertices every frame but I can't convert Vector3 to Vector3[], I tried In many ways but I am not able to solve this
public Vector3 calculatePointOnPlanet(Vector3 directions)
{
float firstLayerValue = 0;
float elevation = 0;
directions = directions;
if (noiseFilters.Length > 0)
{
firstLayerValue = noiseFilters[0].Evaluate(directions);
if (settings.noiseLayers[0].enabled)
{
elevation = firstLayerValue;
}
}
for (int i = 0; i < noiseFilters.Length; i++)
{
if (settings.noiseLayers[i].enabled)
{
float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
elevation += noiseFilters[i].Evaluate(directions) * mask;
}
}
return directions * settings.radius * (1 + elevation);
}
public void a(Vector3[] vertices, Vector3[] directions)
{
for (int i = 0; i < noiseFilters.Length; i++)
{
vertices[i] = calculatePointOnPlanet(directions);
}
}
You probably forgot the index
// |
// V
vertices[i] = calculatePointOnPlanet(directions[i]);
Note btw that
directions = directions
does absolutely nothing

Unity freezes on Play

I've been making this enemy generator, which is called on Start() and it freezes most of the time. Sometimes it let's to play once but as soon as I hit play second time, it freezes, unity needs to be restarted. Standalone version doesn't fix the problem. Logs last lines:
UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
EnemyGenerator:Start() (at Assets\Scripts\Utils\EnemyGenerator.cs:23)
(Filename: Assets/Scripts/Utils/EnemyGenerator.cs Line: 23)
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyGenerator : MonoBehaviour
{
public List<GameObject> camps;
public List<GameObject> enemies;
int threshold;
public List<GameObject> campAreas;
public List<GameObject> enemyAreas;
public int campsToSpawn;
public int enemiesPerAreaMin;
public int enemiesPerAreaMax;
void Start()
{
for (int i = 0; i < campsToSpawn; i++)
{
threshold = Random.Range(0, camps.Count - 1);
Debug.Log(threshold);
var campPlace = Random.Range(0, campAreas.Count - 1);
var camp = Instantiate(camps[threshold], campAreas[campPlace].transform.position, Quaternion.identity);
if (camp != null)
{
campAreas.RemoveAt(campPlace);
}
}
Debug.Break();
bool canSpawn = true;
for (int i = 0; i < enemyAreas.Count; i++)
{
threshold = Random.Range(0, enemies.Count - 1);
List<GameObject> enemiesSpawned = new List<GameObject>();
var enemyCount = Random.Range(enemiesPerAreaMin, enemiesPerAreaMax);
for (int j = 0; j < enemyCount; j++)
{
var pos = new Vector3(Random.Range(enemyAreas[i].GetComponent<BoxCollider>().bounds.min.x, enemyAreas[i].GetComponent<BoxCollider>().bounds.max.x), enemyAreas[i].transform.position.y,
Random.Range(enemyAreas[i].GetComponent<BoxCollider>().bounds.min.z, enemyAreas[i].GetComponent<BoxCollider>().bounds.max.z));
if (enemiesSpawned.Count == 0)
{
GameObject en = null;
enemiesSpawned.Add(en = Instantiate(enemies[threshold], pos, Quaternion.identity));
}
else
{
for (int x = 0; x < enemiesSpawned.Count; x++)
{
if (Vector3.Distance(enemiesSpawned[x].transform.position, pos) < 3f)
{
canSpawn = false;
}
}
if (!canSpawn)
{
j--;
}
else
{
enemiesSpawned.Add(Instantiate(enemies[threshold], pos, Quaternion.identity));
}
}
}
Debug.Break();
}
}
}
I tried to find something that would actually loop forever, but I don't think that's the problem. Wondering if it could be performance leak?
Thank you in advance.
Sry I though it was an issue with Debug.Break();
this is probably the problem
{
for (int x = 0; x < enemiesSpawned.Count; x++)
{
if (Vector3.Distance(enemiesSpawned[x].transform.position, pos) < 3f)
{
canSpawn = false;
}
}
if (!canSpawn)
{
j--;
}
else
{
enemiesSpawned.Add(Instantiate(enemies[threshold], pos, Quaternion.identity));
}
}
You getting a random position inside a certain bounds. If the position is too close u don't spawn and --j; to retry a new position. So if for some reason the bounds are wrong or barely engouth you might be running that function for a very long time.
A quick test is to add a counter before your j for loop.
int debugCounter = 0;
for (int j = 0; j < enemyCount; j++)
{
//at the very top
++debugCounter;
if(debugCounter > 100)//or a bigger number
{
Debug.Log("Too many attemps");
break;
}
...

Trying to create a 3D voxel terrain, and I am trying to create random seeds rather than the same seed multiple times

I am trying to figure out how to randomize my Perlin noise in C# but cannot find a way to do so with the code I currently have. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PerlinCubeGenScript01 : MonoBehaviour {
public float perlinNoise = 0f;
public float refinement = 0f;
public int multiplier = 0;
public int cubes = 0;
public float darkness;
void Start () {
for (int i = 0; i < cubes; i++) {
for (int j = 0; j < cubes; j++) {
perlinNoise = Mathf.PerlinNoise(i * refinement, j * refinement);
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = new Vector3(i, Mathf.Round(perlinNoise * multiplier), j);
int cubeY = (int) Mathf.Round(perlinNoise * multiplier);
Debug.Log(cubeY);
go.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0f);
}
}
}
void Update () {
}
}
As long as Perlin Noise function allows you to pick any point in a 2D space, why not simply use a random number as your seed and start picking points from there?
void Start () {
Random rnd = new Random();
int seed = rnd.Next(Int32.MinValue, Int32.MaxValue);
for (int i = 0; i < cubes; i++) {
for (int j = 0; j < cubes; j++) {
perlinNoise = Mathf.PerlinNoise(seed + (i * refinement), seed + (j * refinement));
// Note that if refinement never changes you are always picking the same point
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = new Vector3(i, Mathf.Round(perlinNoise * multiplier), j);
int cubeY = (int) Mathf.Round(perlinNoise * multiplier);
Debug.Log(cubeY);
go.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0f);
}
}
}

Xna Intersect is to slow

Hello i have a problem with this code:
for (int i = 0; i < map.collision_rects.Count(); i++)
{
if (player.collision_rect.Intersects(map.collision_rects[i]))
{
Debug.WriteLine("INTERSERCTED:" + player.velocity.Y);
player.velocity.Y = 0;
player.has_jumped = false;
}
}
player.Update(gameTime);
The problem is that it Detects that it intersected but the velocity is set to zero a frame later than it detected
if (has_jumped == true)
{
float i = 1;
velocity.Y += 0.15f*i;
}
The code that sets the velocity

Categories

Resources