Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Benr223 · Aug 14, 2014 at 02:17 PM · c#movementrpgcombatturn

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 14, 2014 at 04:14 PM 0
Share

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.

avatar image Benr223 · Aug 15, 2014 at 10:08 AM 0
Share

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.

avatar image robertbu · Aug 16, 2014 at 07:16 AM 0
Share

A coroutine must be called with StartCoroutine(). So you need to do:

 StartCoroutine(Attack(Player.transform));
avatar image Benr223 · Aug 18, 2014 at 09:21 AM 0
Share

That makes so much sense now! Well I feel a bit simple haha, Thanks robertbu, I will try this when I get home :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges