- Home /
Move object to position, perform action, move back to starting position
Hi Everyone,
Currently I am trying to create a turn based battle system in unity, I have all of my state management sorted but my problem is, I want to move my character to the enemy, attack (which calculates damage and displays) then move back, in the future I would like to add an animation to the character whilst moving also. Currently I can get the character to move to the enemy position with a script attached to the object:
IEnumerator Start () {
pointB = new Vector3(targetPos.transform.position.x, targetPos.transform.position.y, targetPos.transform.position.z);
var pointA = transform.position;
yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
Debug.Log(PlayerMoved);
}
void Update () {
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i = 0.0f;
var rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
I on clicking the "attack" button that is coded into my turn based combat script, I enable this script and this works, but the damage is taken off of the enemy as the code does not wait for the script to run its course. After the attack I want to move back to my previous position, I have tried this by making another script and swapping the values PointA and PointB around when calling the move object method, activation and deactivating these scripts don't really act in the way I would like it too, which would be:
if (GUI.Button(new Rect(10, (Screen.height - 75), 80, 20), "Attack"))
{
//Move Towards Player
//Wait till move has completed
enemy1.TakeDamage(Player1.Attack());
//Move Towards original position
//Wait till move has completed
_curreState = BattleStates.ENEMYCHOICE;
}
Thanks in advance guys!
From your description, I'm not sure how you have all this structured...what scripts have access to specific information. Conceptually you want to do this.
IEnumerator Attack() {
yield return StartCoroutine($$anonymous$$oveObject(transform, pointA, pointB, 3.0f));
enemy1.TakeDamage(Player1.Attack());
yield return StartCoroutine($$anonymous$$oveObject(transform, pointB, pointA, 3.0f));
_currState = BattleStates.ENE$$anonymous$$YCHOICE;
}
And your GUI.Button code would start this coroutine. You want to disable the GUI.Button until the player can again attack after launching the coroutine.
I have tried this approach and the method doesn't seem to run:
Attack Button: if (GUI.Button(new Rect(10, (Screen.height - 75), 80, 20), "Attack")) {
Debug.Log("Player attack pressed"); Attack(Player.transform); enemy1.TakeDamage(Player1.Attack()); }
Attack $$anonymous$$ethod:
IEnumerator Attack(Transform player)
{
Debug.Log("Attack method started");
yield return StartCoroutine($$anonymous$$oveObject(player, pointA, pointB, 3.0f));
enemy1.TakeDamage(Player1.Attack());
yield return StartCoroutine($$anonymous$$oveObject(player, pointB, pointA, 3.0f));
_curreState = BattleStates.ENE$$anonymous$$YCHOICE;
}
the debug log does not appear from the attack method when it is run. I am also passing the player game object (my character) so that it knows what to move, as this is a script that is not attached to a physical game object such as a player, but the player is attached to the script.
A coroutine must be called with StartCoroutine(). So you need to do:
StartCoroutine(Attack(Player.transform));
That makes so much sense now! Well I feel a bit simple haha, Thanks robertbu, I will try this when I get home :)
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Multiple Cars not working 1 Answer
Turn Order 2 Answers
Distribute terrain in zones 3 Answers
Hack N' Slash RPG Combat System 2 Answers