I have a cube that is getting each time a new random position and moving to this position.
What i want to do is to draw a line between each last position and the next new position after the cube reached to the new position. And points on each positions. So in the end for example i will have many lines in blue and in red the points that will show the positions.
For example in this draw in paint i did just to show what i mean. The red points display the last position and the next position then from the last red point will be draw another blue line to the next position when the cube reached to make a red filled point/circle.
Let’s say the original start position when running the game will be draw in green the rest will be blue lines and red.
In the end i want to see all the route the cube did and where there was positions started and ended.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
public GameObject objectToPlace;
public Vector3 newObjectsSize = new Vector3(5, 5, 5);
public float speed;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
private int currentObjects;
private List<GameObject> objects = new List<GameObject>();
void Start()
{
var wi = GetComponent<WallsTest>();
wallsLengthX = (int)wi.lengthX;
wallsLengthZ = (int)wi.lengthZ;
wallsPosX = (int)wi.wallsStartPosition.x;
wallsPosZ = (int)wi.wallsStartPosition.z;
}
// Update is called once per frame
void Update()
{
Spawn();
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position,
GenerateRandomPositions(objects[0]), (Mathf.Sin(speed * Time.deltaTime)));
}
private void Spawn()
{
if (currentObjects != numberOfObjects)
{
if (objects.Count != 1) // Why did i make a check if not = 1 ?
{
GameObject newObject = (GameObject)Instantiate(objectToPlace);
newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
newObject.transform.localPosition = GenerateRandomPositions(newObject);
newObject.name = "Spawned Object";
newObject.tag = "Spawned Object";
objects.Add(newObject);
currentObjects += 1;
}
}
}
private Vector3 GenerateRandomPositions(GameObject newObject)
{
float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ) / 2f;
float originX = wallsPosX + paddingX - wallsLengthX / 2f;
float originZ = wallsPosZ + paddingZ - wallsLengthZ / 2f;
float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX);
float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ);
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
return new Vector3(posx, posy, posz);
}
}
This is where i’m moving the cube in the Update:
objects[0].transform.position = Vector3.Lerp(objects[0].transform.position,
GenerateRandomPositions(objects[0]), (Mathf.Sin(speed * Time.deltaTime)));