I want to scale a rectangle made of 4 vertexes in a list simply with the value 0.5. So I mean by that, just make it 0.5 units bigger. The new values should be stored in another list which I named list2 here.
My first thought was trying this:
foreach (Vector3 element in list)
{
if(element.x < 0 && element.z < 0)
{
list2.Add(new Vector3(element.x - 0.5f, 0, element.z - 0.5f));
}
else if(element.x > 0 && element.z > 0)
{
list2.Add(new Vector3(element.x + 0.5f, 0, element.z + 0.5f));
}
else if (element.x < 0 && element.z > 0)
{
list2.Add(new Vector3(element.x - 0.5f, 0, element.z + 0.5f));
}
else if (element.x > 0 && element.z < 0)
{
list2.Add(new Vector3(element.x + 0.5f, 0, element.z - 0.5f));
}
}
I firstly thought that's the way to go, but I realised it would be a problem if the elements are partially equal to 0.
My question now is, is there any way to scale Vector3 points and simply make them bigger or smaller, independing where they are in the coordinate system?
I googled already but only found a given function which helps to scale a Vector but its only scaling by multiplication with a factor and I want to scale it only with 0.5 units.
Maybe you guys can help a noob out. :D
Thanks in advance.
If you already know the order of vertices e.g. in your case counter-clockwise starting at bottom-left I wouldn't use a loop with conditions but rather directly do
public List<Vector3> ScaleRectangleByStep(List<Vector3> vertices, float step)
{
// copy the given list
var output = new List<Vector3>(vertices);
output[0] = output[0] - Vector3.right * step - Vector3.forward * step;
output[1] = output[1] + Vector3.right * step - Vector3.forward * step;
output[2] = output[2] + Vector3.right * step + Vector3.forward * step;
output[3] = output[3] - Vector3.right * step + Vector3.forward * step;
return output;
}
If you don't know the order you would rather somehow get center position in order to check
public List<Vector3> ScaleRectangleByStep(List<Vector3> vertices, float step)
{
// copy the given list
var output = new List<Vector3>(vertices);
// Get the center of the rectangle
var center = Vector3.zero;
foreach(var vertex in output)
{
center += vertex;
}
center /= output.Count;
// in a second run use the center for your conditions
for(var i = 0; i < output.Count; i++)
{
var vertex = output[i];
if(vertex.x < center.x)
{
vertex.x -= step;
}
else if(vertex.x > center.x)
{
vertex.x += step;
}
if(vertex.z < center.z)
{
vertex.z -= step;
}
else if(vertex.z > center.z)
{
vertex.z += step;
}
// the else cases would basically mean that the vertices
// are equal to the center ...
output[i] = vertex;
}
return output;
}
Or as a complete alternative if instead of moving the borders by certain units you rather want to scale by a certain factor I would rather do it like
public List<Vector3> ScaleRectangleByFactor(List<Vector3> vertices, float multiplicationFactor)
{
// copy the given list
var output = new List<Vector3>(vertices);
// first find the center position
var center = Vector3.zero;
foreach(var vertex in output)
{
center += vertex;
}
center /= list.Count;
// Now scale every vertex relative to the center position with the given factor
for(var i = 0; i < output.Count; i++)
{
// Get the position relative to the center
var relativePosition = (output[i] - center);
// scale it up by the given factor
relativePosition *= multiplicationFactor;
// assign it back to the list
output[i] = center + relativePosition;
}
return output;
}
Related
I have a coroutine DefaultFixedUpdate() that functions as a replacement for FixedUpdate() and another coroutine called WallSlidingFixedUpdate(). I have two variables that store which of the both is active. activeCoroutine and prevActiveCoroutine with the starting strings nameof(DefaultFixedUpdate) and "no coroutine". In FixedUpdate() from Unity I have this code that should change coroutines if activeCoroutine and prevActiveCoroutine are not the same:
void FixedUpdate()
{
if (activeCoroutine != prevActiveCoroutine)
{
print($"changing coroutines from {prevActiveCoroutine} to {activeCoroutine}");
StopCoroutine(prevActiveCoroutine);
StartCoroutine(activeCoroutine);
prevActiveCoroutine = activeCoroutine;
print("changed coroutines");
}
}
When starting the game DefaultFixedUpdate() starts as expected and prevActiveCoroutine changes as expected to activeCoroutine and when a condition inside DefaultFixedUpdate() is met to switch to WallSlidingFixedUpdate() activeCoroutine changes to the new string as expected. But when StartCoroutine(activeCoroutine) gets called the code inside WallSlidingFixedUpdate() doesn't run for some reason. StopCoroutine(prevActiveCoroutine) does work. The end result is that none of the two corutines runs but expected was that WallSlidingCoroutine() runs.
These are the two Coroutines. The changing of activeCoroutine happens almost at the bottom of both functions.
IEnumerator DefaultFixedUpdate()
{
while (true)
{
//setup
gravity = (velocity.y < 0 || !Input.GetKey(KeyCode.Space)) ? dropGravity : normalGravity;
Vector2 pos = transform.position + col.size.y * 0.5f * Vector3.up;
Vector2 timeStepVelocity = velocity * Time.fixedDeltaTime;
Vector2 pos1 = pos;
velocity.y -= gravity * Time.fixedDeltaTime;
//checking collision
RaycastHit2D[] colChecks = Physics2D.BoxCastAll(pos, col.size, 0, timeStepVelocity, timeStepVelocity.magnitude, 0b1000000);
//remove unwanted collisions that are behind and don't make sense to collide with
List<RaycastHit2D> newColChecks = new();
foreach (RaycastHit2D colCheck in colChecks)
if (Vector2.Dot(colCheck.normal, timeStepVelocity) < 0)
newColChecks.Add(colCheck);
colChecks = newColChecks.ToArray();
//remove unwanted velocities
foreach (RaycastHit2D colCheck in colChecks)
{
if (Mathf.Abs(colCheck.normal.x / colCheck.normal.y) > 1) // left and right
{
velocity.x = Mathf.Sign(colCheck.normal.x) > 0 ? Mathf.Max(velocity.x, 0) : Mathf.Min(velocity.x, 0);
}
else // top bottom
{
velocity.y = Mathf.Sign(colCheck.normal.y) > 0 ? Mathf.Max(velocity.y, 0) : Mathf.Min(velocity.y, 0);
}
}
//calculate correct destination
if (colChecks.Length == 0)
pos += timeStepVelocity;
else
{
//create bounding box for all possible future positions
Vector4 posBox = new Vector4(pos.x, pos.x + timeStepVelocity.x, pos.y, pos.y + timeStepVelocity.y);
//move to surface
pos = colChecks[0].centroid;
//slide along surface until hitting another tangent or the bounding box
Vector2 tangent = new Vector3(colChecks[0].normal.y, -colChecks[0].normal.x);
Vector2 centroid = pos;
//slide until hitting distance bounds
if (Mathf.Abs(tangent.x) > Mathf.Abs(tangent.y)) //top and bottom
{
pos += tangent / tangent.x * (timeStepVelocity.x - pos.x + pos1.x);
tangent = Mathf.Sign(centroid.y - pos1.y) * tangent;
}
else //left and right
{
pos += tangent / tangent.y * (timeStepVelocity.y - pos.y + pos1.y);
tangent = Mathf.Sign(centroid.x - pos1.x) * tangent;
}
//save previous surface that is being slid on
Vector2 prevTangent = Tangent(colChecks[0].normal);
//check if I collided with another tangent along the way
colChecks = Physics2D.BoxCastAll(centroid, col.size, 0, pos, Vector2.Distance(pos, centroid), 0b1000000);
//remove unwanted collisions that are behind and don't make sense to collide with
newColChecks = new();
foreach (RaycastHit2D colCheck in colChecks)
if (Vector2.Dot(colCheck.normal, tangent * Mathf.Sign(Vector2.Dot(pos - centroid, tangent))) < 0 || Vector2.Dot(colCheck.normal, timeStepVelocity) < 0)
newColChecks.Add(colCheck);
colChecks = newColChecks.ToArray();
if (colChecks.Length > 1)
{
pos = centroid + (pos - centroid).normalized * colChecks[1].distance;
}
//repeat
int loopBreak = 0;
string loopBreakMessage = "";
while (true)
{
loopBreakMessage += centroid + "\n";
if (loopBreak > 4)
{
Debug.LogError("loop limit exceeded\n" + loopBreakMessage);
break;
}
loopBreak++;
//check if I collided with another tangent along the way
colChecks = Physics2D.BoxCastAll(centroid, col.size, 0, pos - centroid, Vector2.Distance(pos, centroid), 0b1000000);
//remove unwanted collisions that are behind and don't make sense to collide with
newColChecks = new();
foreach (RaycastHit2D colCheck in colChecks)
if (Tangent(colCheck.normal) != prevTangent)
newColChecks.Add(colCheck);
colChecks = newColChecks.ToArray();
if (colChecks.Length > 2)
{
prevTangent = colChecks[0].normal;
if (!VectorOnSameQuarter(colChecks[1].normal, colChecks[2].normal)) // stuck in a corner
{
pos = centroid + (pos - centroid).normalized * colChecks[2].distance;
break;
}
else //continue along new tangent
{
//corner pos
centroid += (pos - centroid).normalized * colChecks[2].distance;
//end pos if there's no surface along the way
pos = PosAlongVectorInsideRectangle(posBox, centroid, Tangent(colChecks[2].normal));
}
}
else
{
break;
}
}
AlignVelocityWithGround();
}
//check if player is grounded and save ground tangent to remove small hops when going down slopes
PlayerState.OnGround = false;
RaycastHit2D[] jumpChecks = Physics2D.BoxCastAll(pos, col.size, 0, Vector2.down, 1e-3f, 0b1000000);
if (jumpChecks.Length != 0)
{
foreach (RaycastHit2D jumpCheck in jumpChecks)
{
if (jumpCheck.normal.y > Mathf.Abs(jumpCheck.normal.x))
{
PlayerState.OnGround = true;
groundTangent = new Vector2(jumpCheck.normal.y, -jumpCheck.normal.x);
}
}
PlayerState.state = PlayerState.state == PlayerState.State.WallSliding ? PlayerState.State.Default : PlayerState.state;
//check for a wall slide and save wall tangent for smooth sliding
if (!PlayerState.OnGround && velocity.y < 0 && Mathf.Abs(jumpChecks[0].normal.x) > Mathf.Abs(jumpChecks[0].normal.y) && Mathf.Asin(jumpChecks[0].normal.y) * Mathf.Rad2Deg < 15)
{
groundTangent = new Vector2(jumpChecks[0].normal.y, -jumpChecks[0].normal.x);
transform.position = pos - col.size.y * 0.5f * Vector2.up;
activeCoroutine = nameof(WallSlidingFixedUpdate);
yield break;
}
}
transform.position = pos - col.size.y * 0.5f * Vector2.up;
yield return new WaitForFixedUpdate();
}
}
IEnumerable WallSlidingFixedUpdate()
{
print("wallsliding coroutine entered");
while (true)
{
print("wallsliding coroutine happening");
Vector2 pos = transform.position;
pos -= Time.fixedDeltaTime * wallSlideSpeed * groundTangent / groundTangent.y;
transform.position = pos;
if (InputManager.Left && groundTangent.y > 0)
{
activeCoroutine = nameof(DefaultFixedUpdate);
yield break;
}
else if (InputManager.Right)
{
activeCoroutine = nameof(DefaultFixedUpdate);
yield break;
}
yield return new WaitForFixedUpdate();
}
}
You figured your issue about the return type out already -> It needs to be IEnumerator.
Anyway my question is WHY?
Instead of going through all that trouble for switching between different Coroutine instances via a string I would rather have only a single outer scope routine running and then rather switch between which inner block to execute
private enum RoutineType
{
Default,
WallSliding
}
private RouineType activeRoutine;
private IEnumerator FixedUptadeRoutine()
{
while(true)
{
switch(activeRoutine)
{
case RoutineType.WallSliding:
yield return DefaultRoutine();
break;
case default:
yield return WallSlidingRoutine();
break;
}
}
}
private IEnumerator DefaultRoutine()
{
while(true)
{
// NOTE: Btw you want to wait for fixed update FIRST
// Before applying all the values
...
// And now this will simply make sure you change the routine type
activeRoutine = RoutineType.WallSliding;
// and exit this one so the outer routine will handle the switch in the next frame
yield break;
...
}
}
private IEnumerator WallSlidingRoutine()
{
...
}
you can simply yield return any IEnumerator so it will be executed and at the same time the outer routine waits for it to finish. There is no need to have multiple start and stop coroutines.
Holy moly, how did I miss this. The return value of WallSlidingFixedUpdate is IEnumerable and not IEnumerator. I changed it to IEnumertor and now it works.
How do I instantiate as many gameObjects as possible that start at one position and end at another position. For example, instantiate gameObject at x=0 and end at x=5 axis. Between these two values, there should be as many gameObjects as possible, preferably 10-12 small scaled ones.
public GameObject prefab;
void Awake()
{
GameObject ref = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
}
when you say as many GameObjects as possible I guess you mean without overlapping?
This solution works assuming the prefab uses Colliders.
I would instantiate the first object allways and simply get it's bounding box so we know how big it is
var first = Instantiate(prefab, Vector3.zero + Vector3.right * MinX, Quaternion.identity);
var bounds = new Bounds(Vector3.zero, Vector3.zero);
foreach (var col in first.GetComponentsInChildren<Collider>(true))
{
bounds.Encapsulate(col.bounds);
}
// now you can get the size in X direction
var width = bounds.size.x;
I suspect the pivot point of your prefab would probably be in the center so first move it to the right by half of its width
first.transform.position += Vector3.right * width / 2f;
Now you can check how many objects will fit in your given range. Lets say e.g. the width was 1 then in a range from 0 to 5 there would fit in total 4 objects. There will be some redundancies in the calculation (adding 1 then decreasing 1 etc ) but I'll leave it sfor better understanding
var minPosition = MinX;
var maxPosition = MaxX;
var actualMinPosition = minPosition + width / 2;
var actualMaxPosition = maxPosition - width / 2;
// +1 here since before we reduced actualMinPosition and actualMaxPosition by
// exactly 1 * width
var possibleAmount = (int)Mathf.Floor((actualMaxPosition - actualMinPosition) / width) + 1;
So now instantiate the missing objects
// since I guess you also want them evenly spread between the start and end position
var distanceBetween = (actualMaxPosition - actualMinPosition) / (possibleAmount - 1);
// since we already instantiated the first one
// we spawn only possibleAmount - 1 more
for (var i = 0; i < possibleAmount - 1; i++)
{
// +1 here since we started the loop with i=0 but the first
// object here is actually the second to be spawned in total
// so we want it to be moved already
var x = actualMinPosition + distanceBetween * (i + 1);
var obj = Instantiate(prefab, Vector3.zero + Vector3.right * x, Quaternion.identity);
}
So all together
public void Spawn()
{
var first = Instantiate(prefab, Vector3.zero, Quaternion.identity);
var bounds = new Bounds(Vector3.zero, Vector3.zero);
foreach (var col in first.GetComponentsInChildren<Collider>(true))
{
bounds.Encapsulate(col.bounds);
}
// now you can get the size in X direction
var width = bounds.size.x;
first.transform.position += Vector3.right * width / 2f;
var minPosition = MinX;
var maxPosition = MaxX;
var actualMinPosition = minPosition + width / 2;
var actualMaxPosition = maxPosition - width / 2;
// +1 here since before we reduced actualMinPosition and actualMaxPosition by
// exactly 1 * width
var possibleAmount = (int)Mathf.Floor((actualMaxPosition - actualMinPosition) / width) + 1;
// since I guess you also want them evenly spread between the start and end position
var distanceBetween = (actualMaxPosition - actualMinPosition) / (possibleAmount - 1);
// since we already instantiated the first one
// we spawn only possibleAmount - 1 more
for (var i = 0; i < possibleAmount - 1; i++)
{
// +1 here since we started the loop with i=0 but the first
// object here is actually the second to be spawned in total
// so we want it to be moved already
var x = actualMinPosition + distanceBetween * (i + 1);
var obj = Instantiate(prefab, Vector3.zero + Vector3.right * x, Quaternion.identity);
}
}
I simply destroyed and respawned everything in Update for this demo
You could loop the amount of enemys that you want to be spawned(12 for example) and increase the position at every loop iteration.
public GameObject prefab;
public Vector3 pos;
void Awake()
{
for (int i = 0; i < 12; i++)
{
Instantiate(prefab, pos, Quaternion.identity);
pos.x += 0.5f;
}
}
This should create 10 GameObject between 0 and 5.
void Awake() {
for(float x = 0; x < 5; x+=0.5f){
Vector3 loc = new Vector3(x, 0, 0);
GameObject gameObject = (GameObject)Instantiate(prefab, loc, Quaternion.identity);
}
}
Thank you for all your answers, I did something like this:
(a) Create two gameObjects in the scene separated by some distance
(b) In the script, give reference to these two gameObjects
(c) Give number of segments (spheres) that should be generated between these two points
public Transform PointA; public Transform PointB; public float NumberOfSegments = 3; public float AlongThePath = .25f;
// Update is called once per frame
void Start()
{
Create();
}
void Create()
{
StartCoroutine(StartSpheringOut());
}
IEnumerator StartSpheringOut()
{
NumberOfSegments += 1;// since we are skipping 1st placement since its the same as starting point we increase the number by 1
AlongThePath = 1 / (NumberOfSegments);//% along the path
for (int i = 1; i < NumberOfSegments; i++)
{
yield return new WaitForSeconds(0.05f);
Vector3 CreatPosition = PointA.position + (PointB.position - PointA.position) * (AlongThePath * i);
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = CreatPosition;
sphere.transform.localScale = new Vector3(0.25f, 0.25f, 0.25f);
}
I have made a line in the following way:
public class MyLineRenderer : MonoBehaviour {
LineRenderer lineRenderer;
public Vector3 p0, p1;
// Use this for initialization
void Start () {
lineRenderer = gameObject.GetComponent<LineRenderer>();
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, p0);
lineRenderer.SetPosition(1, p1);
}
}
How can I find, for example, 10 points on the line that are equally spaced from end to the other?
You can use Vector3.Lerp to generate a point between two points. Passing 0.5 to its t parameter will make it give you the middle position between PositionA and PositionB.
To generate multiple points between two points, you just have to use Vector3.Lerp in loop.
Here is a function to do this:
void generatePoints(Vector3 from, Vector3 to, Vector3[] result, int chunkAmount)
{
//divider must be between 0 and 1
float divider = 1f / chunkAmount;
float linear = 0f;
if (chunkAmount == 0)
{
Debug.LogError("chunkAmount Distance must be > 0 instead of " + chunkAmount);
return;
}
if (chunkAmount == 1)
{
result[0] = Vector3.Lerp(from, to, 0.5f); //Return half/middle point
return;
}
for (int i = 0; i < chunkAmount; i++)
{
if (i == 0)
{
linear = divider / 2;
}
else
{
linear += divider; //Add the divider to it to get the next distance
}
// Debug.Log("Loop " + i + ", is " + linear);
result[i] = Vector3.Lerp(from, to, linear);
}
}
USAGE:
//The two positions to generate point between
Vector3 positionA = new Vector3(0, 0, 0);
Vector3 positionB = new Vector3(254, 210, 50);
//The number of points to generate
const int pointsCount = 10;
//Where to store those number of points
private Vector3[] pointsResult;
void Start()
{
pointsResult = new Vector3[pointsCount];
generatePoints(positionA, positionB, pointsResult, pointsCount);
}
The 10 different array points are now stored in the pointsResult variable.
If you want to include the first point of the line renderer in the array, you can do the following:
int amnt = 10; // if you want 10 points
Vector3[] points = new Vector3[amnt]; // your array of points
for (int x = 0; x < amnt; x++)
{
points[x] = new Vector3((p1.x - p0.x) * x / (amnt-1),
(p1.y - p0.y) * x / (amnt-1),
(p1.z - p0.z) * x / (amnt-1)); // we divide by amnt - 1 here because we start at 0
}
Ok so this site has helped me alot before i had an account, but now the net is not helping me so i'll post it myself :)
I am looking (i am yet another one) to do something like this:
tinykeep.com/dungen/
Now the first step(Generating the rooms , goes well), but this is the first time ever I encounter Flocking behaviour algorithms, and my maths really arent that terrific.
So i tried and came up with a non working algorithm, what i did is started from next standard separation algorithm: gamedevelopment.tutsplus.com/tutorials/the-three-simple-rules-of-flocking-behaviors-alignment-cohesion-and-separation--gamedev-3444 (sorry for this text but it wouldn't let me post more than 2 links without 10 rep), And changed it to my liking. Like i said before it's really not doing anything, if i find how i'll post some debug results.
Here's my code:
class Room {
Vector3 position;
Vector3 size;
int area;
public Room(Vector3 position, Vector3 size, int area) : this(position, size){
this.area = area;
}
public Room(Vector3 position, Vector3 size){
this.position = position;
this.size = size;
if(area != null)
this.area = (int)(size.x * size.z);
}
public Vector3 GetPosition(){
return position;
}
public Vector3 GetSize(){
return size;
}
public void SetPosition(Vector3 position){
this.position = position;
}
public Boolean TooCloseTo(Room room){
return position.x + size.x / 2 > room.GetPosition().x - room.GetSize().x / 2 || position.z + size.z / 2 > room.GetPosition().z - room.GetSize().z / 2 ||
position.x - size.x / 2 < room.GetPosition().x + room.GetSize().x / 2 || position.z - size.z / 2 < room.GetPosition().z + room.GetSize().z / 2;
}
public override string ToString ()
{
return string.Format ("[Room]\nPosition:{0}\nSize:{1}\nArea:{2}", position, size, area);
}
}
These are part of the Mapgenerator class, the rest of this class works fine and seems unimportant to me, but if you feel the need to see it, let me know and i'll post it.
void SeparateRooms(){
foreach (Room room in rooms) {
//Debug.Log (room);
Vector3 oldPos = room.GetPosition ();
Vector3 separation = computeSeparation (room);
//Debug.Log (separation);
Vector3 newPos = new Vector3 (oldPos.x += separation.x, oldPos.y, oldPos.z += separation.z);
room.SetPosition (newPos);
//Debug.Log (room);
}
}
Vector3 computeSeparation(Room agent) {
int neighbours = 0;
Vector3 v = new Vector3 ();
foreach (Room room in rooms) {
if (room != agent) {
if (agent.TooCloseTo (room)) {
v.x += Difference(room, agent, "x");
v.z += Difference(room, agent, "z");
neighbours++;
}
}
}
if (neighbours == 0)
return v;
v.x /= neighbours;
v.z /= neighbours;
v.x *= -1;
v.z *= -1;
v.Normalize ();
return v;
}
float Difference(Room room, Room agent, string type){
switch (type) {
case "x":
float xBottom = (room.GetPosition ().x + room.GetSize ().x / 2) - (agent.GetPosition ().x - agent.GetSize ().x / 2);
float xTop = (agent.GetPosition ().x + agent.GetSize ().x / 2) - (room.GetPosition ().x - room.GetSize ().x / 2);
return xBottom > 0 ? xBottom : xTop;
break;
case "z":
float xRight= (room.GetPosition ().z + room.GetSize ().z / 2) - (agent.GetPosition ().z - agent.GetSize ().z / 2);
float xLeft= (agent.GetPosition ().z + agent.GetSize ().z / 2) - (room.GetPosition ().z - room.GetSize ().z / 2);
return xRight > 0 ? xRight : xLeft;
default:
return 0;
break;
}
}
I was expecting the difference to be numbers like 4.0 and 8.0 and so on, instead i get 0.9 and 0.3 and so on, i'll copy an output:
Debug log
I hope that is readable.
Explanation: Each three lines are the info about the room:
1. the original room
2. the (what's supposed to be the) overlap between the room and the next
3. the new position of the room
EDIT: Ok thanks to the comments i'm getting pretty normal results for overlap now, but the rooms are still overlapping after i run the algorithm, i think this might have to do something with only iterating once over every room... Any comments on that?
I really hope some of you might be able to point me in the right direction, or tell me what is wrong in my reasoning.
Thank you!
EDIT: Filling up rooms:
public void Generate(int level) {
int levelModifier = (int)(Math.Round ((double)Mathf.Log (level, 2.5f),1) * 10);
int mean = 25;
int SD = 5;
for (int i = 0; i < 30 + levelModifier; i++) {
Vector3 position = getPInCircle (20);
int area = (int)SimpleRNG.GetNormal (mean, SD);
if (area < mean - SD)
continue;
Vector3 size = calculateSize (area);
Room room = new Room (position, size);
rooms.Add (room);
}
SeparateRooms ();
}
Vector3 calculateSize(int area) {
float k = UnityEngine.Random.Range (2.0f, area / 2.0f);
float l = area / k;
int shortSide;
int longSide;
if (Mathf.Round (k) > Mathf.Round (l)) {
shortSide = Mathf.RoundToInt (k);
longSide = Mathf.RoundToInt (l);
} else {
shortSide = Mathf.RoundToInt (l);
longSide = Mathf.RoundToInt (k);
}
//Debug.Log (shortSide);
//Debug.Log (longSide);
Vector3 size;
if (SimpleRNG.GetUniform () < 0.5) {
size = new Vector3 (Actualise(shortSide), 0, Actualise(longSide));
} else {
size = new Vector3 (Actualise(longSide), 0, Actualise(shortSide));
}
return size;
}
The SimpleRNG is John D. Cook's Namespace, open for download at www.codeproject.com/Articles/25172/Simple-Random-Number-Generation
I have just done the Curves and Splines Tutorials from the catlikecoding.com website (http://catlikecoding.com/unity/tutorials/curves-and-splines/) and after finishing it I came up with an idea. I would like to spawn the items from the SplineDecorator at the same distance from the spline at both sides of the spline. I have tried duplicating the for loop and instantiating the newItem twice but at different position but it doenst work as I want to. Here's the Spline Decorator script that takes a spline and then Instantiate some items along the its path
using UnityEngine;
public class SplineDecorator : MonoBehaviour
{
public BezierSpline spline;
public int frequency;
public float distanceBetweenItems;
public bool lookForward;
public Transform[] items;
private void Awake()
{
if (frequency <= 0 || items == null || items.Length == 0)
{
return;
}
float stepSize = 1f / (frequency * items.Length);
for (int p = 0, f = 0; f < frequency; f++)
{
for (int i = 0; i < items.Length; i++, p++)
{
Transform item = Instantiate(items[i]) as Transform;
Vector3 position = spline.GetPoint(p * stepSize);
item.transform.localPosition = new Vector3(position.x + distanceBetweenItems, position.y, position.z);
if (lookForward)
{
item.transform.LookAt(position + spline.GetDirection(p * stepSize));
}
item.transform.parent = transform;
}
}
}
}
You don't define exactly what "at both sides" means to you, so I'm not sure if this is what you're looking for, but maybe this will get you on the right track.
I replaced the inner loop with the following and got a sort of "race track" feel with distanceBetweenItems = 2:
Transform item = Instantiate(items[i]) as Transform;
Transform item2 = Instantiate(items[i]) as Transform;
Vector3 position = spline.GetPoint(p * stepSize);
Vector3 direction = spline.GetDirection(p * stepSize);
Vector3 cross = Vector3.Cross(direction, Vector3.up);
Vector3 delta = cross.normalized * (distanceBetweenItems/2);
item.transform.localPosition = new Vector3(position.x + delta.x, position.y + delta.y, position.z + delta.z);
item2.transform.localPosition = new Vector3(position.x - delta.x, position.y - delta.y, position.z - delta.z);
if (lookForward)
{
item.transform.LookAt( position + spline.GetDirection(p * stepSize));
item2.transform.LookAt(position + spline.GetDirection(p * stepSize));
}
item.transform.parent = transform;
item2.transform.parent = transform;
What I've done here is use Vector3.Cross() (Unity Documentation) to find the line perpendicular to both the spline and Vector3.up. Then I calculate how far along that line to go by normalizing it and multiplying it by half of distanceBetweenItems. (So that, combined, they're distanceBetweenItems apart.)
If you instead wanted something like tracing the wingtips of a plane flying that spline, you'll need to replace the Vector.up above. The simplest way is to replace it with the 'binormal vector', though there are issues with that. This pdf I just found talks a little about it, and might get you on the right path.
(Goofing around, I was able to get a reasonable approximation by replacing the Vector3 cross = ... line with Vector3 cross = Vector3.Cross(direction, direction - spline.GetDirection(p * stepSize + 0.01f));, though it's screwy on sharp bends, non-mirrored vertices, and at the start/end loop.)