0
\$\begingroup\$

I have two characters"agent" moving them on terrain by mouse. My script for both of them is...

public Vector3 point_;
public float mark;
public float mark;
public UI_Button _active_character; // butoon select script

void Update 
{

  Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit mousePoint;
    if (Input.GetMouseButtonUp(0))
    {
        if (Physics.Raycast(mouseRay, out mousePoint, 100) )
        {
            point_ = mousePoint.point;
            mAgent.SetDestination(mousePoint.point);
            mark = Vector3.Distance(point_, gameObject.transform.position);
        }
    }
 }

Here script for character buttons. Every character has it own button.

public bool character1, character2;

void Start()
{
    character1 = true;

}

   void Update()
{

    if (character1 == true)
    {
        character2 = false;
    }

    if (character2 == true)
    {
        character1 = false;
    }

   if (character1 == fale && character2 == false)
    {
    // Later. free cam moving...
    }

}

public void avatar1_click() // button 
{
    character1 = !character1;
    character2 = false;

}
public void avatar2_click() // button
{
    character2 = !character2;
    character1 = false;
}

It's possible to control both of them separately by one script by click on button to chose character ?

\$\endgroup\$
  • 1
    \$\begingroup\$ your naming conventions hurt my heart :) \$\endgroup\$ – Evorlor Jan 6 '19 at 21:44
1
\$\begingroup\$

Just try to adjust first script and make it identify which player "object" is active when click.

   if (_active_character.character1 == true && this.gameObject.name == "Player1 Object Name")
    {
        Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit mousePoint;
        if (Input.GetMouseButtonUp(0))
        {
            if (Physics.Raycast(mouseRay, out mousePoint, 100))
            {
                point_ = mousePoint.point;
                this.mAgent.SetDestination(mousePoint.point);
                mark = Vector3.Distance(point_, gameObject.transform.position);
            }
        }

    }

if (_active_character.character2 == true && this.gameObject.name == "Player2 Object Name")
    {
        Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit mousePoint;
        if (Input.GetMouseButtonUp(0))
        {
            if (Physics.Raycast(mouseRay, out mousePoint, 100))
            {
                point_ = mousePoint.point;
                this.mAgent.SetDestination(mousePoint.point);
                mark = Vector3.Distance(point_, gameObject.transform.position);
            }
        }

    }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.