How can i create a squad of soldiers in specific formation ?
I tried this script but it's not working. For now i have only one formation square. I want for example if there are 4 columns and space 10 and 20 soldiers so the soldiers will be in formation of 4 soldiers in a row(column) and 5 lines.
And if for example in the loop i will make 23 so there will be 4 soldiers in a row/column and 6 lines the last line will be with only 3 soldiers.
In example1 it's a case of 4 soldiers in a column and 5 lines(columns).
In this example2 there are 23 soldiers so the last lines have only 3 soldiers. This is how the squarre format should be.
The script i tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadFormation : MonoBehaviour
{
enum Formation
{
Square
}
public Transform squadMemeber;
public int columns = 4;
public int space = 10;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void ChangeFormation()
{
Formation formation = Formation.Square;
switch (formation)
{
case Formation.Square:
for (int i = 0; i < 20; i++)
{
Transform go = Instantiate(squadMemeber);
Vector2 pos = CalcPosition(i);
go.position = new Vector2(pos.x, pos.y);
}
break;
}
}
Vector2 CalcPosition(int index) // call this func for all your objects
{
float posX = (index % columns) * space;
float posY = (index / columns) * space;
return new Vector2(posX, posY);
}
}
But it does nothing. Not event creating the soldiers. I'm not getting any errors or exceptions. And not sure if i should use Vector2 or Vector3 but it's not working at all. Even not duplicate/creating the soldiers.
Answer by akillingbeck · Sep 05, 2017 at 12:12 PM
You're not calling the ChangeFormation() function for starters
This is working, almost working as i wanted.
It looks like there are 4 lines and in each line 5 soldiers. But they should facing to the left so it will be like 5 lines and each line 4 soldiers.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadFormation : $$anonymous$$onoBehaviour
{
enum Formation
{
Square
}
public Transform squad$$anonymous$$emeber;
public int columns = 4;
public int space = 10;
// Use this for initialization
void Start()
{
ChangeFormation();
}
// Update is called once per frame
void Update()
{
}
private void ChangeFormation()
{
Formation formation = Formation.Square;
switch (formation)
{
case Formation.Square:
for (int i = 0; i < 20; i++)
{
Transform go = Instantiate(squad$$anonymous$$emeber);
Vector3 pos = CalcPosition(i);
go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
}
break;
}
}
Vector2 CalcPosition(int index) // call this func for all your objects
{
float posX = (index % columns) * space;
float posY = (index / columns) * space;
return new Vector2(posX, posY);
}
}
I don't understand. And I can't really see the image. Do you mean the soliders should be facing a direction?
You Haven't Set The Rotation In The Code, So They Probably Have (0,0,0) Rotation! You Have To Add It To The Instantiation Line:
for (int i = 0; i < 20; i++)
{
Transform go = Instantiate(squad$$anonymous$$emeber);
Vector3 pos = CalcPosition(i);
go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
//ADDED THE LINE BELOW:
go.rotation = Quaternion.Euler(new Vector3(-90,0,0));
}
Answer by hiuzhanwei · Sep 04, 2019 at 09:19 PM
Hi All,
For learning purposes, is there anything wrong if i do it this way?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
enum Formation
{
Square
}
public GameObject squadMember;
public int columns = 4;
public int space = 3;
// Use this for initialization
void Start()
{
ChangeFormation();
}
// Update is called once per frame
void Update()
{
}
private void ChangeFormation()
{
Formation formation = Formation.Square;
switch (formation)
{
case Formation.Square:
for (int i = 0; i < 20; i++)
{
Instantiate(squadMember);
Vector3 pos = CalcPosition(i);
squadMember.transform.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
}
break;
}
}
Vector3 CalcPosition(int index) // call this func for all your objects
{
float posX = (index % columns) * space;
float posY = (index / columns) * space;
return new Vector2(posX, posY);
}
}
Your answer
Follow this Question
Related Questions
How can i disable/enable slowly the blur effect using the blur script ? 0 Answers
How can I give each instance a enum mode from another script ? 0 Answers
How can i make the right choice of implementation of my shooting fire script ? 1 Answer
How can i set the camera to be automatic behind the player ? 0 Answers
can i get help please 0 Answers