Infinite Scroll Menu Unity - c#

So I have created a menu for my game with UI text boxes(TMP now) and DOTween with infinite scrolling for them . Is there an optimized way to achieve this or can the script be optimized further?.
The Script used:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class movelist : MonoBehaviour
{
[SerializeField] List<RectTransform> children = new List<RectTransform>();
Vector2 temp;
int childcount;
[SerializeField] int tohighlight, tosetactive = 0;
[SerializeField] float lerptime = .5f;
[SerializeField] bool working;
private void Start()
{
working = false;
childcount = transform.childCount;
tohighlight = childcount / 2;
for (int i = 0; i < childcount; i++)
children.Add(transform.GetChild(i).GetComponent<RectTransform>());
children[tohighlight].GetComponent<Animator>().enabled = true;
}
private void Update()
{
if (working)
return;
if (Input.GetKey(KeyCode.UpArrow) || Input.mouseScrollDelta.y < 0)
move(-1);
if (Input.GetKey(KeyCode.DownArrow) || Input.mouseScrollDelta.y > 0)
move(1);
if (Input.GetKey(KeyCode.KeypadEnter) || Input.GetKey(KeyCode.Space))
children[tohighlight].GetComponent<Button>().onClick.Invoke();
}
private void move(int dir)
{
working = true;
if (dir == 1)
StartCoroutine(moveup());
else
StartCoroutine(movedown());
}
private IEnumerator moveup()
{
tosetactive -= 1;
if (tosetactive < 0)
tosetactive = childcount + tosetactive;
children[tosetactive].gameObject.SetActive(false);
temp = children[0].anchoredPosition;
yield return null;
for (int i = 0; i < childcount - 1; i++)
{
children[i].DOLocalMove(children[i + 1].anchoredPosition, lerptime, true);
yield return null;
}
children[childcount - 1].DOLocalMove(temp, lerptime, true).OnComplete(() => changeBool(tosetactive));
yield return null;
children[tohighlight].GetComponent<Animator>().enabled = false;
tohighlight = (tohighlight - 1) % childcount;
if (tohighlight < 0)
tohighlight = childcount + tohighlight;
children[tohighlight].GetComponent<Animator>().enabled = true;
}
private IEnumerator movedown()
{
temp = children[childcount - 1].anchoredPosition;
children[tosetactive].gameObject.SetActive(false);
yield return null;
for (int i = childcount - 1; i > 0; i--)
{
children[i].DOLocalMove(children[i - 1].anchoredPosition, lerptime, true);
yield return null;
}
children[0].DOLocalMove(temp, lerptime, true).OnComplete(() => changeBool(tosetactive, 1));
yield return null;
children[tohighlight].GetComponent<Animator>().enabled = false;
tohighlight = (tohighlight + 1) % childcount;
children[tohighlight].GetComponent<Animator>().enabled = true;
}
void changeBool(int n, int v = -1)
{
working = false;
children[n].gameObject.SetActive(true);
if (v != -1)
tosetactive = (tosetactive + 1) % childcount;
}
}
Let me explain it once so at Start I get all the children of the Canvas element and store there reference in a list. The children are kept in the hierarchy so as the center child is at the center of the screen. On the respective button or scroll of up or down the list is shifted up or down keeping in the mind(SetActive(False)) the last or the first element so that the user does not see that moving on the screen.
GIF: CLICK HERE
Any help is greatly appreciated.

Related

Random Bug in Unity2D

I've been working on a project for the GMTK 2022 Game Jam recently, and I ran into a very strange problem. I have a dash that starts when you are moving and press space. It moves you in the direction of your velocity, then for a short time lets you move very quickly. It works perfectly fine in all cases, unless the direction you are moving is up and to the left, in which case, the if statement strangely won't trigger. I'm sure this is something idiotic, but I've been troubleshooting it for the last hour and it's been driving me insane.
// Update is called once per frame
void Update()
{
playerInputh = 0;
playerInputv = 0;
if (Input.GetKey("right"))
{
playerInputh = 1;
}
if (Input.GetKey("left"))
{
playerInputh = -1;
}
if (Input.GetKey("right") && Input.GetKey("left"))
{
playerInputh = 0;
}
if (Input.GetKey("up"))
{
playerInputv = 1;
}
if (Input.GetKey("down"))
{
playerInputv = -1;
}
if (Input.GetKey("up") && Input.GetKey("down"))
{
playerInputv = 0;
}
Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
//This is the dash that isn't working:
if ((Input.GetKeyDown(/*"right shift"*/"space")) && (playerInputh != 0 || playerInputv != 0))
{
Debug.Log("Dash");
//Vector2 transform2dposition = new Vector2(transform.position.x, transform.position.y);
m_Rigidbody.AddForce((m_Rigidbody.velocity) * 500f);
wJumpTimer = airControlAfterJump;
speed = maxSpeed*3.5f;
StartCoroutine(Roll());
}
}
void FixedUpdate()
{
//no moving while jumping!!!
if (wJumpTimer > 0)
{
wJumpTimer -= 1;
}
else
{
wJumpTimer = 0;
}
//move
if (playerInputh != 0 && playerInputv != 0) //make diagonals no super sayan
{
playerInputh *= moveLimiter;
playerInputv *= moveLimiter;
one_h = playerInputh;//futureproof
one_v = playerInputv;
}
if ((playerInputh != 0 || playerInputv != 0) && speed < maxSpeed) //are we hitting the move buttons??
{
speed += acceleration;//accelerate
one_h = playerInputh;//futureproof
one_v = playerInputv;
}
else
{
if (speed > 0f) //are we getting off the ride
{
speed -= deceleration; //decelerate
}
else
{
speed = 0f; //no funny buisness
}
}
m_Rigidbody.velocity = new Vector2(one_h * speed, one_v * speed); //actually move
}
void SetFace(int diceNumb)
{
rndr.sprite = sprites[diceNumb];
}
IEnumerator Roll()
{
Random diceNumb = new Random();
rndr.sprite = sprites[diceNumb.Next(0,5)];
yield return new WaitForSeconds(0.125f);
rndr.sprite = sprites[diceNumb.Next(0, 5)];
yield return new WaitForSeconds(0.125f);
rndr.sprite = sprites[diceNumb.Next(0, 5)];
yield return new WaitForSeconds(0.125f);
rndr.sprite = sprites[diceNumb.Next(0, 5)];
yield return new WaitForSeconds(0.125f);
var newValue = diceNumb.Next(0, 5);
FaceValue = newValue + 1;
rndr.sprite = sprites[newValue];
}
How are your inputs setup? I am suspecting you have one key bound to multiple actions, like one key is set up as primary for an action and alternate for another action.
Personally I'd also stop using the assignment operators and increment instead. Instead of
playerInputh = 0;
if (Input.GetKey("right"))
{
playerInputh = 1;
}
if (Input.GetKey("left"))
{
playerInputh = -1;
}
if (Input.GetKey("right") && Input.GetKey("left"))
{
playerInputh = 0;
}
you can do
playerInputv = 0;
if (Input.GetKey("right"))
{
playerInputh += 1;
}
if (Input.GetKey("left"))
{
playerInputh += -1;
}
Net result is the same here - if you push both keys the result sums to zero, but the code is easier to read (IMO).
When you check things for key bindings also check alternates for space, because that's another one of the triggers you need to dash.

Unity Error building Player because scripts had compiler errors

I know this is a well-known question. But I don't know what to do.
(Open the image in a new tab for better clarity)
That script FastIKFabric.cs is working perfectly (moreover it's from asset store) and as you can see in the inspector it does not have any using UnityEditor;. The line 250 is the last one from that script and the } is closed properly. As I said, the script does not have any errors.
What can I do? Thanks in advance for any answer! :)
Here is the script:
#if UNITY_EDITOR
#endif
using UnityEngine;
namespace DitzelGames.FastIK
{
/// <summary>
/// Fabrik IK Solver
/// </summary>
public class FastIKFabric : MonoBehaviour
{
/// <summary>
/// Chain length of bones
/// </summary>
public int ChainLength = 2;
/// <summary>
/// Target the chain should bent to
/// </summary>
public Transform Target;
public Transform Pole;
/// <summary>
/// Solver iterations per update
/// </summary>
[Header("Solver Parameters")]
public int Iterations = 10;
/// <summary>
/// Distance when the solver stops
/// </summary>
public float Delta = 0.001f;
/// <summary>
/// Strength of going back to the start position.
/// </summary>
[Range(0, 1)]
public float SnapBackStrength = 1f;
protected float[] BonesLength; //Target to Origin
protected float CompleteLength;
protected Transform[] Bones;
protected Vector3[] Positions;
protected Vector3[] StartDirectionSucc;
protected Quaternion[] StartRotationBone;
protected Quaternion StartRotationTarget;
protected Transform Root;
// Start is called before the first frame update
void Awake()
{
Init();
}
void Init()
{
//initial array
Bones = new Transform[ChainLength + 1];
Positions = new Vector3[ChainLength + 1];
BonesLength = new float[ChainLength];
StartDirectionSucc = new Vector3[ChainLength + 1];
StartRotationBone = new Quaternion[ChainLength + 1];
//find root
Root = transform;
for (var i = 0; i <= ChainLength; i++)
{
if (Root == null)
throw new UnityException("The chain value is longer than the ancestor chain!");
Root = Root.parent;
}
//init target
if (Target == null)
{
Target = new GameObject(gameObject.name + " Target").transform;
SetPositionRootSpace(Target, GetPositionRootSpace(transform));
}
StartRotationTarget = GetRotationRootSpace(Target);
//init data
var current = transform;
CompleteLength = 0;
for (var i = Bones.Length - 1; i >= 0; i--)
{
Bones[i] = current;
StartRotationBone[i] = GetRotationRootSpace(current);
if (i == Bones.Length - 1)
{
//leaf
StartDirectionSucc[i] = GetPositionRootSpace(Target) - GetPositionRootSpace(current);
}
else
{
//mid bone
StartDirectionSucc[i] = GetPositionRootSpace(Bones[i + 1]) - GetPositionRootSpace(current);
BonesLength[i] = StartDirectionSucc[i].magnitude;
CompleteLength += BonesLength[i];
}
current = current.parent;
}
}
// Update is called once per frame
void LateUpdate()
{
ResolveIK();
}
private void ResolveIK()
{
if (Target == null)
return;
if (BonesLength.Length != ChainLength)
Init();
//Fabric
// root
// (bone0) (bonelen 0) (bone1) (bonelen 1) (bone2)...
// x--------------------x--------------------x---...
//get position
for (int i = 0; i < Bones.Length; i++)
Positions[i] = GetPositionRootSpace(Bones[i]);
var targetPosition = GetPositionRootSpace(Target);
var targetRotation = GetRotationRootSpace(Target);
//1st is possible to reach?
if ((targetPosition - GetPositionRootSpace(Bones[0])).sqrMagnitude >= CompleteLength * CompleteLength)
{
//just strech it
var direction = (targetPosition - Positions[0]).normalized;
//set everything after root
for (int i = 1; i < Positions.Length; i++)
Positions[i] = Positions[i - 1] + direction * BonesLength[i - 1];
}
else
{
for (int i = 0; i < Positions.Length - 1; i++)
Positions[i + 1] = Vector3.Lerp(Positions[i + 1], Positions[i] + StartDirectionSucc[i], SnapBackStrength);
for (int iteration = 0; iteration < Iterations; iteration++)
{
//https://www.youtube.com/watch?v=UNoX65PRehA
//back
for (int i = Positions.Length - 1; i > 0; i--)
{
if (i == Positions.Length - 1)
Positions[i] = targetPosition; //set it to target
else
Positions[i] = Positions[i + 1] + (Positions[i] - Positions[i + 1]).normalized * BonesLength[i]; //set in line on distance
}
//forward
for (int i = 1; i < Positions.Length; i++)
Positions[i] = Positions[i - 1] + (Positions[i] - Positions[i - 1]).normalized * BonesLength[i - 1];
//close enough?
if ((Positions[Positions.Length - 1] - targetPosition).sqrMagnitude < Delta * Delta)
break;
}
}
//move towards pole
if (Pole != null)
{
var polePosition = GetPositionRootSpace(Pole);
for (int i = 1; i < Positions.Length - 1; i++)
{
var plane = new Plane(Positions[i + 1] - Positions[i - 1], Positions[i - 1]);
var projectedPole = plane.ClosestPointOnPlane(polePosition);
var projectedBone = plane.ClosestPointOnPlane(Positions[i]);
var angle = Vector3.SignedAngle(projectedBone - Positions[i - 1], projectedPole - Positions[i - 1], plane.normal);
Positions[i] = Quaternion.AngleAxis(angle, plane.normal) * (Positions[i] - Positions[i - 1]) + Positions[i - 1];
}
}
//set position & rotation
for (int i = 0; i < Positions.Length; i++)
{
if (i == Positions.Length - 1)
SetRotationRootSpace(Bones[i], Quaternion.Inverse(targetRotation) * StartRotationTarget * Quaternion.Inverse(StartRotationBone[i]));
else
SetRotationRootSpace(Bones[i], Quaternion.FromToRotation(StartDirectionSucc[i], Positions[i + 1] - Positions[i]) * Quaternion.Inverse(StartRotationBone[i]));
SetPositionRootSpace(Bones[i], Positions[i]);
}
}
private Vector3 GetPositionRootSpace(Transform current)
{
if (Root == null)
return current.position;
else
return Quaternion.Inverse(Root.rotation) * (current.position - Root.position);
}
private void SetPositionRootSpace(Transform current, Vector3 position)
{
if (Root == null)
current.position = position;
else
current.position = Root.rotation * position + Root.position;
}
private Quaternion GetRotationRootSpace(Transform current)
{
//inverse(after) * before => rot: before -> after
if (Root == null)
return current.rotation;
else
return Quaternion.Inverse(current.rotation) * Root.rotation;
}
private void SetRotationRootSpace(Transform current, Quaternion rotation)
{
if (Root == null)
current.rotation = rotation;
else
current.rotation = Root.rotation * rotation;
}
void OnDrawGizmos()
{
#if UNITY_EDITOR
var current = this.transform;
for (int i = 0; i < ChainLength && current != null && current.parent != null; i++)
{
var scale = Vector3.Distance(current.position, current.parent.position) * 0.1f;
//Handles.matrix = Matrix4x4.TRS(current.position, Quaternion.FromToRotation(Vector3.up, current.parent.position - current.position), new Vector3(scale, Vector3.Distance(current.parent.position, current.position), scale));
//Handles.color = Color.green;
//Handles.DrawWireCube(Vector3.up * 0.5f, Vector3.one);
current = current.parent;
}
}
#endif
}
}
I don't believe you, the compiler doesn't lie. Perhaps you closed it properly, but did you save the script in visual studio before rebuilding??
Just 2 cents. A long answer just to illustrate the problem and make it easier for others to see.
Take a minute and think about the preprocessor directive (#if UNITY_EDITOR), so, if we are inside/using the UnityEditor the code inside the directives (#if...#endif) will be active.
So since we have:
// previous code ...
void OnDrawGizmos()
{
#if UNITY_EDITOR
var current = this.transform;
for (int i = 0; i < ChainLength && current != null && current.parent != null; i++)
{
// ...code...
}
}
#endif
// rest of the code ...
private void LikeAnotherMethod()
{
....
}
While we are in UNITY_EDITOR will be equal to;
// previous code ...
void OnDrawGizmos()
{
var current = this.transform;
for (int i = 0; i < ChainLength && current != null && current.parent != null; i++)
{
// ...code...
}
}
// rest of the code ...
private void LikeAnotherMethod()
{
....
}
But, if we are not on UnityEditor's world (like on an Android, iOS, or MacOSX game) everything inside those preprocessor directives will be gone.
So, in our case;
// previous code ...
void OnDrawGizmos()
{
#if UNITY_EDITOR
// nope var current = this.transform;
// nope for (int i = 0; i < ChainLength && current != null && current.parent != null; i++)
// nope {
// nope // ...code...
// nope }
// nope }
#endif
// rest of the code ...
private void LikeAnotherMethod()
{
....
}
which is the same as;
// previous code ...
void OnDrawGizmos()
{
// rest of the code ...
private void LikeAnotherMethod()
{
....
}
So in the last block of code is easier to see that before the //rest of the code a { is missing. This is the cause of the problem.
VisualStudio (or other IDEs will not complain in the meanwhile because inside UnityEditor world everything is ok, but once you are building the app it will fail.)
A possible fix will be to move the #endif preprocessor directive one line up:
// previous code ...
void OnDrawGizmos()
{
#if UNITY_EDITOR
var current = this.transform;
for (int i = 0; i < ChainLength && current != null && current.parent != null; i++)
{
// ...code...
}
#endif
}
// rest of the code ...
private void LikeAnotherMethod()
{
....
}

How can I scale all the objects in the array at the same time?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLinesAnimated : MonoBehaviour
{
public Transform[] objectsToScale;
private void Start()
{
for (int i = 0; i < objectsToScale.Length; i++)
{
StartCoroutine(scaleOverTime(objectsToScale[i], new Vector3(2, objectsToScale[i].transform.localScale.y, objectsToScale[i].transform.localScale.z), 2));
}
}
bool isScaling = false;
IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
//Make sure there is only one instance of this function running
if (isScaling)
{
yield break; ///exit if this is still running
}
isScaling = true;
float counter = 0;
//Get the current scale of the object to be moved
Vector3 startScaleSize = objectToScale.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
isScaling = false;
}
}
I tried using a loop :
for (int i = 0; i < objectsToScale.Length; i++)
But it's scaling only the first object in the array.
Do it all in the coroutine.. (unchecked code, may have odd errors but..)
This will run through all items in the array a bit like you tried..
If you want in parallell you'd need to funk it up a little.
Enumerator scaleOverTime(float ScaleSize, float duration)
{
if (isScaling) yield return break;
isScaling = true;
for (int i = 0; i < objectsToScale.Length; i++)
{
float counter = 0;
//Get the current scale of the object to be moved
Vector3 startScaleSize = objectToScale[i].localScale;
Vector3 toScale = startScale*ScaleSize;
while (counter < duration)
{
counter += Time.deltaTime;
objectToScale[i].localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
}
isScaling = false;
}

How to prevent drag and drop if the node is not null [C#]

I have a problem with drag and drop on game. I want to block user if he is dragging the object from down to up in case they have sticks in same angles. if they dont so it is ok. But if they have even one stick in same direction so stick should go back to down to first position.
I am lost a bit. Can you help please :)
here is the code where I drag and drop also rotation...
Here on the pic generated stick group can fit only 1. and 3. places.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputController : MonoBehaviour
{
float clickTime;
RaycastHit2D rayhit;
float smallestDistance = 1.5f;
int smallestId = 1;
public Transform[] nodes;
public LayerMask selectableObjLayerMask;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickTime = Time.time;
rayhit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, selectableObjLayerMask);
}
else if (Input.GetMouseButtonUp(0))
{
if (rayhit)
{
if (Time.time - clickTime < .2f)
{
Node node = rayhit.transform.GetComponent<Node>();
if (node != null)
{
for (int i = 0; i < node.sticks.Count; i++)
{
Vector3 newAngles = new Vector3(0, 0, (node.sticks[i].transform.localEulerAngles.z - 45));
newAngles.z = newAngles.z < 0 ? newAngles.z + 180 : newAngles.z;
newAngles.z = newAngles.z >180 ? newAngles.z - 180 : newAngles.z;
node.sticks[i].transform.localEulerAngles = newAngles;
node.sticks[i].degree = (int)newAngles.z;
Debug.Log(i + " = " + node.sticks[i].degree);
}
}
}
else
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode.isMoved == false)
{
smallestId = 0;
smallestDistance = 999;
for (int i = 0; i < nodes.Length; i++)
{
float distance = Vector2.Distance(rayhit.transform.position, nodes[i].transform.position);
if (smallestDistance > distance)
{
smallestDistance = distance;
smallestId = i;
Debug.Log("Obje : " + currNode);
}
}
rayhit.transform.position = nodes[smallestId].transform.position;
if (rayhit.transform.parent != nodes[smallestId].transform)
{
if (nodes[smallestId].transform.childCount > 0 && nodes[smallestId].transform != rayhit.transform.parent)
{
if (currNode != null)
{
for (int i = 0; i < currNode.sticks.Count; i++)
{
nodes[smallestId].transform.GetChild(0).GetComponent<Node>().sticks.Add(currNode.sticks[i]);
currNode.sticks[i].transform.SetParent(nodes[smallestId].transform.GetChild(0));
}
Destroy(rayhit.transform.gameObject);
}
}
else
{
if (currNode != null)
{
currNode.isMoved = true;
}
rayhit.transform.SetParent(nodes[smallestId].transform);
}
}
}
}
}
rayhit = new RaycastHit2D();
}
else if (Input.GetMouseButton(0))
{
if(rayhit.transform != null)
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode != null)
if (currNode.isMoved == false)
{
if (Time.time - clickTime >= 0.2f)
{
Vector2 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rayhit.transform.position = newPos;
}
}
}
}
}
}

How can I add more lights if twoSides flag is true?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsEffect : MonoBehaviour
{
public List<UnityEngine.GameObject> waypoints;
public int howmanylight = 5;
public bool changeLightsDirection = false;
public bool twoSides = false;
public float delay = 0.1f;
private List<UnityEngine.GameObject> objects;
private Renderer[] renderers;
private int greenIndex = 0;
private float lastChangeTime;
private void Start()
{
objects = new List<UnityEngine.GameObject>();
if (howmanylight > 0)
{
UnityEngine.GameObject go1 = UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Sphere);
duplicateObject(go1, howmanylight);
LightsEffects();
}
}
private void Update()
{
LightsEffectCore();
}
public void duplicateObject(UnityEngine.GameObject original, int howmany)
{
if (twoSides == true)
howmany = howmany * 2;
howmany++;
for (int i = 0; i < waypoints.Count - 1; i++)
{
for (int j = 1; j < howmany; j++)
{
Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
UnityEngine.GameObject go = Instantiate(original, new Vector3(position.x, 0.05f, position.z), Quaternion.identity);
go.transform.localScale = new Vector3(0.3f, 0.1f, 0.3f);
objects.Add(go);
}
}
}
private void LightsEffects()
{
renderers = new Renderer[objects.Count];
for (int i = 0; i < renderers.Length; i++)
{
renderers[i] = objects[i].GetComponent<Renderer>();
renderers[i].material.color = Color.red;
}
// Set green color to the first one
greenIndex = 0;
renderers[greenIndex].material.color = Color.green;
}
private void LightsEffectCore()
{
// Change color each `delay` seconds
if (Time.time > lastChangeTime + delay)
{
lastChangeTime = Time.time;
// Set color of the last renderer to red
// and the color of the current one to green
renderers[greenIndex].material.color = Color.red;
if (changeLightsDirection == true)
{
Array.Reverse(renderers);
changeLightsDirection = false;
}
greenIndex = (greenIndex + 1) % renderers.Length;
renderers[greenIndex].material.color = Color.green;
}
}
}
Inside the method duplicateObject I'm checking if the flag twoSides is true and then double by twice the amount of lights:
public void duplicateObject(UnityEngine.GameObject original, int howmany)
{
if (twoSides == true)
howmany = howmany * 2;
howmany++;
for (int i = 0; i < waypoints.Count - 1; i++)
{
for (int j = 1; j < howmany; j++)
{
Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
UnityEngine.GameObject go = Instantiate(original, new Vector3(position.x, 0.05f, position.z), Quaternion.identity);
go.transform.localScale = new Vector3(0.3f, 0.1f, 0.3f);
objects.Add(go);
}
}
}
This lines:
if (twoSides == true)
howmany = howmany * 2;
But this will add just more 5 lights between the waypoints like it show in the screenshot.
And I want it to add more 5 lights but to create two lines between the waypoints of lights line by line.
Somehow to change the new 5 lights position so it will be near/next the first 5 lights. And then to align the lines of lights so they will be equal between the waypoints.
Two lines and two sides I mean something like this:
But they should be equal between the waypoints like in the first screenshot.
What I tried is this:
Changed the duplicateObject method to:
public void duplicateObject(UnityEngine.GameObject original, int howmany)
{
GameObject go = new GameObject();
if (twoSides == true)
howmany = howmany * 2;
howmany++;
for (int i = 0; i < waypoints.Count - 1; i++)
{
for (int j = 1; j < howmany; j++)
{
Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
if (j > 5)
{
go = Instantiate(original, new Vector3(position.x + 1, 0.05f, position.z), Quaternion.identity);
}
else
{
go = Instantiate(original, new Vector3(position.x, 0.05f, position.z), Quaternion.identity);
}
go.transform.localScale = new Vector3(0.3f, 0.1f, 0.3f);
objects.Add(go);
}
}
}
But the result is not what I wanted:
Now the problem is that the lights the gaps between them is not equal between the two waypoints (cubes) and also the new 5 lights are two far from the waypoints.
Both lights each 5 lights should be with equal gaps and side by side between the waypoints. Like a landing lights for aircraft between the two waypoints.

Categories