make a gameobject move to another gameobject that was selected by mouse click?
Im currently making a turn based type of game however im stuck figuring out how to move the player to the position of the enemy that was chosen.
This is the player script with the positions of the enemies.
void Update()
{
if ((Input.GetKeyDown("1")) && (BattleFlow.playerTurn == 1)) // attack monster 1
{
BattleFlow.currentDamage = 40;
GetComponent<Animator>().SetTrigger("PlayerMelee");
GetComponent<Transform>().position = new Vector2(-4.78f, 3.68f);
StartCoroutine(playerAttack());
}
else if ((Input.GetKeyDown("1")) && (BattleFlow.playerTurn == 1)) // attack monster 2
{
GetComponent<Animator>().SetTrigger("PlayerMelee");
GetComponent<Transform>().position = new Vector2(-4.45f, 1.77f);
StartCoroutine(playerAttack2());
}
}
The player can move to the location of monster 1 however the script for monster 2 doesn't work. The player still goes towards the direction of monster 1 even though I clicked monster 2.
This is the code for the mouse click which I placed on the enemy script.
// Update is called once per frame
void Update () {
void OnMouseDown()
{
Debug.Log(gameObject.name);
BattleFlow.selectedEnemy = gameObject.name;
}
}
Can any help me with the scripting? Much appreciated.
Answer by testserver00 · Aug 16, 2017 at 10:19 AM
Figured It out just now. Placed the name of the monster that was selected.
if (((Input.GetKeyDown("1")) && (BattleFlow.playerTurn == 1)) && (BattleFlow.selectedEnemy == "slime")) // monster 1
if (((Input.GetKeyDown("1")) && (BattleFlow.playerTurn == 1)) && (BattleFlow.selectedEnemy == "bat"))//monster 2
working fine now. But if anyone has a different solution feel free to post. Thanks
Your answer
Follow this Question
Related Questions
How attach C# script by argument? 3 Answers
Player should turn in the direction the player is running. (2D Game) 0 Answers
Having trouble moving a GameObject to another scene and Transform it to a different position 0 Answers
How can I change GameObject's texture? 1 Answer
My character won't stop crouching, they dont stop crouchin 2 Answers