- Home /
Move n-Objects in a row to a point "B", looking at it
Hi, I have already figured out how to place a number of n cubes, all looking at the same point B. But the issue here is, that they don't spawn in a row, which is also looking at point B (row-direction to B). The Position of Point B is variable before runtime. I already thank you in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rings : MonoBehaviour {
float lerptime = 5;
public float currentLerpTime = 0;
public Transform ring, RingClone;
public GameObject PointA, PointB, RingPrefab;
Vector3 startPos;
Vector3 endPos;
public int Kapaz;
public int i = 0;
public int capacity;
public string KnotA, KnotB;
int n;
public string KanalObjekt;
void Start() {
}
void Update()
{
//Test
CreateRings();
}
public void getNodes(string A, string B)
{
KnodeA = A;
KnodeB = B;
}
//capacity = Number of Cubes to be spawned
public void getCapacity(int capacity)
{
Kapaz = capacity;
}
public void CreateRings()
{
//capacity = Number of Cubes to be spawned
getCapacity(5);
//Nodes
getNodes("Node A", "Node B");
PointA = GameObject.Find(KnodeA);
PointB = GameObject.Find(KnodeB);
//Ring = Parent Cube
ring = transform.Find("Ring");
ring.LookAt(PointB.transform);
//RingPrefab = Child Cubes (Parent = Ring)
RingPrefab = GameObject.Find("RingPrefab");
Vector3 ringpos = ring.transform.position;
if (i != Kapaz)
{
for (n = Kapaz; n > 0; n--)
{
Vector3 clonepos = new Vector3(ringpos.x + (0.25f * n) , ringpos.y, ringpos.z);
Instantiate(RingPrefab).transform.parent = ring.transform;
RingClone = ring.transform.Find("RingPrefab(Clone)");
RingClone.name = n.ToString();
RingClone.transform.position = clonepos;
RingClone.LookAt(PointB.transform);
}
//only executed if kapaz has been changed
i = Kapaz;
}
//move row from A to B
startPos = PointA.transform.position;
ring.transform.position = startPos;
endPos = PointB.transform.position;
currentLerpTime += Time.deltaTime;
if (currentLerpTime >= lerptime)
{
currentLerpTime = lerptime;
}
float perc = currentLerpTime / lerptime;
ring.transform.position = Vector3.Lerp(startPos, endPos, perc);
//cube back to point A
if (ring.transform.position == PointB.transform.position)
{
currentLerpTime = 0;
}
}
}
Can you please draw an image in paint of what you would like it to look like
Answer by unity_TQ4BNk5-bqnogg · Jan 14, 2019 at 09:28 PM
So the red cubes should basicly be behind the blue one, also (as the blue cube) looking at Point B (the sphere).
Your answer
Follow this Question
Related Questions
Leading Reticle 0 Answers
Smooth swapping my game object 0 Answers
3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers
MoveTowards is not working with RectTransform component 0 Answers