Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jan 11, 2020 at 01:28 AM by TheShameDeveloper for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by TheShameDeveloper · Dec 02, 2018 at 08:50 PM · animationbooleans

How to apply same script to only one game object at a time...

I'm fairly new to Unity and #C programming. I am creating a turn-based strategy game. Each team has a "commander" and nine units each. Every unit (including the commander) on each team is using the same script to move across the board. This script includes a "playerMoving" variable (bool) to pass into the animator scripts. When this variable is true, a running animation would play as the unit is moving across the gird. Unfortunately, because this script is attached to each unit, every unit's running animation will play even though only one is moving.

I tried creating different animation scripts for each unit and different animators for each unit, but that doesn't seem to solve the issue. It would appear that the issue would be in my "PlayerMove" script that is attached to every unit. How would I specify that only the current game object's playerMove variable should be set to true? Here is my PlayerMove script:

 public class PlayerMove : TacticsMove {

 GameObject target;
 GameObject aiUnit;
 GameObject playerUnit;
 public static bool playerMoving = false;
 public static bool playerAttacking = false;
 public static bool skipUnit = false;

 private static int attackCount = 0;

 // Use this for initialization
 void Start ()
 {
     Init();

     //Anim = GetComponent<Animator>;

 }


 // Update is called once per frame
 void Update()
 {
     Debug.DrawRay(transform.position, transform.forward);

     if (!turn)
     {
         return;
     }

     if (newUnitTurn)
     {   // Just got turn. Find path and start moving
         FindSelectableTiles(gameObject);
         CheckMouse();
         playerMoving = false;
         newUnitTurn = false;
         moving = false;
         NPCMove.NPCMoving = false;
         return;
     }

     if (moving)
     {   // Continue moving to target tile
         Move();
         playerMoving = true;
         NPCMove.NPCMoving = false;
         NPCMove.NPC_Attacking = false;
         return;
     }
     else
     {
         CheckMouse();
     }

     if (attacking)
     {
         playerAttacking = true;
         PlayerAttacksNPC(aiUnit);
         Debug.Log("Target unit; " + aiUnit);
     }

     if (skipUnit)
     {
         skipUnit = false;
         DontMove();
     }


 }

 // Check whether left mouse clicked. Here, if mouse click is on an enemy unit
 // in a reachable target, set playerAttacking flag.
 void CheckMouse()
 {
     NPCMove.NPCMoving = false;
     NPCMove.NPC_Attacking = false;

     bool skipUnit = false;
     if (Input.GetKeyDown("space"))
     {
         StartCoroutine(WaitTime(0.3f));
         skipUnit = true;
     }
     else if (Input.GetMouseButtonUp(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             Debug.Log("Mouse hit " + hit.collider.tag);
             if (hit.collider.tag == "Tile")
             {
                 Tile t = hit.collider.GetComponent<Tile>();
                 Debug.Log("Tile selectable " + t.selectable);
                 if (t.selectable)
                 {
                     moving = true;
                     MoveToTile(t);
                 }
             }
             else if (hit.collider.tag == "NPC")
             {   // Will attack NPC pointed by mouse click. 
                 Tile t = GetTargetTile(hit.collider.gameObject);
                 target = hit.collider.gameObject;
                 aiUnit = t.GetUnitObject();

                 // Calculate the distance if it is less than range start moving
                 float npcDistance = Vector3.Distance(transform.position, target.transform.position);

                 if (npcDistance < this.GetComponent<Unit>().GetRange() + 1)
                 {
                     moving = true;
                     willAttackAfterMove = true;
                     DontMove();
                 }
                 else if (npcDistance <= this.GetComponent<Unit>().GetMove() + this.GetComponent<Unit>().GetRange())
                 {
                     moving = true;
                     // Find next selectable tile from adjacency list aand move to it
                     willAttackAfterMove = true;      // Set player attacking mode
                     MoveToSelectableNeighborTile(t, this.GetComponent<Unit>().GetRange(), 
 this.gameObject);
                 }

             }
         }
     }
 }


 void PlayerAttacksNPC(GameObject aiUnit)
 {   // Player Attacks NPC pointed by mouse click
     //Debug.Log("Player attacking NPC........");
     //PlayerCombat.SetStats(soldier.Unit.GetAttack(), soldier.Unit.GetDefense, target.Unit.GetAttack(), target.Unit.GetDefense); FIX LATER*****
     PlayerCombat pc = new PlayerCombat();
     //pc.SetStats(5, 5, 5, 5);
     
     int dmg = pc.AttackPhase(true, this.GetComponent<Unit>().GetAttack(), this.GetComponent<Unit>().GetDefense(), aiUnit.GetComponent<Unit>().GetAttack(), aiUnit.GetComponent<Unit>().GetDefense());
     aiUnit.GetComponent<Unit>().TakeDmg(dmg);
     Debug.Log("Player hits for: " + dmg);
     Debug.Log("Player has: " + this.GetComponent<Unit>().GetHealth());

     attacking = false;
     willAttackAfterMove = false;
     StartCoroutine(WaitTime(1.0f));
     //Debug.Log("I done waited");
     //TurnManager.EndTurn();

     /* attackCount++;      // Increase attack count for testing
      if (attackCount < 5)
      {
          Debug.Log("Player hits for: " + dmg);
      }
      else
      {
          attackCount = 0;

          // Run following when attack completes for turning to another unit
          Debug.Log("Player attacks ended....Turning to another unit");
          attacking = false;
          TurnManager.EndTurn();
      }*/
 }

 IEnumerator WaitTime(float sec)
 {
     print(Time.time);
     yield return new WaitForSeconds(sec); //This Command doesn't get activated; why not?
        // Time.timeScale = 0;
     TurnManager.EndTurn(); //Supposedly it must be called from the CoRountinue what ever it is you want to happen
 }

 }

Here is my PlayerAnimator script:

   public class PlayerAnimController : MonoBehaviour {

 private Animator playerAnim;

 // Use this for initialization
 void Start () {
     this.playerAnim = this.GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void Update ()
 {

     if (PlayerMove.playerMoving)
     {
         playerAnim.SetBool("Running", true);
     }
     else
     {
         playerAnim.SetBool("Running", false);
     }

     if (PlayerMove.playerAttacking)
     {
         playerAnim.SetBool("Attacking", true);
     }
     else
     {
        playerAnim.SetBool("Attacking", false);
     }

 }

 public void SetAttackingPlayer()
 {
     PlayerMove.playerAttacking = false;
 }
 }  

(I apologize for the line spacing).

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Pakillottk · Dec 02, 2018 at 09:56 PM

Hello, well the problem it's that you've made the variables as static, so the values are shared across all units. If you need to have it as static for whatever reason, I think you could solve It changing the variable from bool to a reference to the Unit's GameObject. So, when nothing it's moving, the variable would be null. If the player selects one unit and orders a move then the variable should change to that unit GameObject so you could access only that unit's animator.

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

249 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What am I doing wrong? Boolean-Animation Help please 1 Answer

Can I make animations snap to a frame? 1 Answer

Animation Script not working. 1 Answer

How to select an animation clip by index number? 7 Answers

Animation Question 1 Answer


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