- Home /
Select and move 1 object, problem = moving multiple.
Hello
I am trying to setup a RTS type system where i can select a npc and move it around, It works fine when i have 1 npc in the scene to move, But if i duplicate that npc and select only 1 and move it, it will move all the npc's i did not select.
Q. What am i missing here? Thanks.
private NavMeshAgent agent;
private Transform constructor;
private Animation anim;
public bool selected;
void Start ()
{
selected = false;
agent = gameObject.GetComponent<NavMeshAgent> ();
constructor = transform.FindChild("Constructor");
anim = constructor.GetComponent<Animation>();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "Player")
{
selected = true;
}else{
selected = false;
}
}
}
if (Input.GetMouseButtonDown(1) && selected)
{
if (Physics.Raycast(ray, out hit))
{
agent.SetDestination(hit.point);
}
}
Answer by FairGamesProductions · Nov 08, 2014 at 02:24 AM
I am assuming that you attach this script to the NPC? And the NPC has a tag called "Player"? If these 2 statements are true, then YES, when one of the NPCs is clicked, both "feel" it and act as if selected. The casting of the ray should be done in a separate script. Here is the script for the NPCs (JS):
private var selected = false;
private var agent : NavMeshAgent;
private var anim : Animator;
private var MoveTo : Vector3;
private var NotSet = false;
function Start ()
{
anim = gameObject.transform.FindChild("Constructor").GetComponent(Animator);
agent = gameObject.GetComponent(NavMeshAgent);
}
function Update ()
{
if (selected && NotSet)
{
NotSet = false;
agent.SetDestination(MoveTo);
anim.SetTrigger("Move"); //I assume the NPC has a move animation
}
if (agent.pathStatus == NavMeshPathStatus.PathComplete)
{
anim.SetTrigger("Idle");
}
}
function Select (x : int)
{
selected = true;
}
function Deselect (x : int)
{
selected = false;
}
function Destination (d : Vector3)
{
MoveTo = d;
NotSet = true;
}
And attached to another (unrelated) GameObject, like the Camera:
private var selectedUnit : GameObject;
function Update ()
{
var hit : RaycastHit;
var ray: Ray = gameObject.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
if (hit.transform.tag == "Player" && Input.GetMouseButtonDown(0))
{
selectedUnit = hit.transform.gameObject;
selectedUnit.SendMessage("Select", 1);
}
if (hit.transform.tag == "Ground" && Input.GetMouseButtonDown(1))
{
selectedUnit.SendMessage("Destination", hit.point);
}
if (hit.transform.tag == "Ground" && Input.GetMouseButtonDown(0))
{
selectedUnit.SendMessage("Deselect", 1);
}
}
}
To work on multiple units keep an array of the selected gameObjects, and use a for loop (example js):
for (i in SelectedUnits)
{
i.SendMessage("Select", 1);
}
Thanks allot for the help, Been pulling my hair out over this. I am no good with js but i managed to convert it to c# incase anyone else stumbles onto this answer.
Thanks again.
private bool selected = false;
private Nav$$anonymous$$eshAgent agent;
private Animator anim;
private Vector3 $$anonymous$$oveTo;
private bool $$anonymous$$ove = false;
void Start (){
anim = gameObject.transform.FindChild("Constructor").GetComponent<Animator>();
agent = gameObject.GetComponent<Nav$$anonymous$$eshAgent>();
}
void Update (){
if (selected && $$anonymous$$ove)
{
agent.SetDestination($$anonymous$$oveTo);
}
if (agent.pathStatus == Nav$$anonymous$$eshPathStatus.PathComplete)
{
$$anonymous$$ove = false;
}
}
void Select (int x){
selected = true;
}
void Deselect (int x){
selected = false;
}
void Destination (Vector3 d){
$$anonymous$$oveTo = d;
$$anonymous$$ove = true;
}
and
void Update (){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Player" && Input.Get$$anonymous$$ouseButtonDown(0))
{
selectedUnit = hit.transform.gameObject;
selectedUnit.Send$$anonymous$$essage("Select", 1);
}
if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(1))
{
selectedUnit.Send$$anonymous$$essage("Destination", hit.point);
}
if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(0))
{
selectedUnit.Send$$anonymous$$essage("Deselect", 1);
}
}
}
just one thing, $$anonymous$$ake sure to set $$anonymous$$ove = false; in the first if. Otherwise agent.SetDestination($$anonymous$$oveTo); will happen every frame until it reaches the destination.
(See the revised code above)
What I gave you was the start of the code you need. It obviously needs to be more complex for a full fledged strategy game. Just send a Deselect message to the currently selected until before sending the Select message to the new one (js):
private var selectedUnit : GameObject;
function Update ()
{
var hit : RaycastHit;
var ray: Ray = gameObject.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
if (hit.transform.tag == "Player" && Input.Get$$anonymous$$ouseButtonDown(0))
{
if (selectedUnit != null)
{
selectedUnit.Send$$anonymous$$essage("Deselect", 1); //This will deselect the previously selected unit (if any)
}
selectedUnit = hit.transform.gameObject;
selectedUnit.Send$$anonymous$$essage("Select", 1);
}
if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(1))
{
selectedUnit.Send$$anonymous$$essage("Destination", hit.point);
}
if (hit.transform.tag == "Ground" && Input.Get$$anonymous$$ouseButtonDown(0))
{
selectedUnit.Send$$anonymous$$essage("Deselect", 1);
}
}
}
Thanks again, I am in a learning phase at the moment, Nothing i do is for commercial use, Its more of a self taught hobby, What i tend to do is try and achieve something myself, if i get stuck get help, remember what i have been told then start again, try and replicate what i did remembering my faults. So thanks again much appreciated.
Your answer
Follow this Question
Related Questions
RPG style character with multiple items 1 Answer
How to add gameobject inside of an array? 0 Answers
more efficient = gameObject w. Material[5] or 5x GameObject w. material[1] 1 Answer
Reverting to prefab-state several game objects at once with stock Unity 3 Answers
Is it possible to add a script to multiple objects seleced in the inspector? 1 Answer