Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Big problem

Discussion in 'Scripting' started by michele2000bon, Jan 13, 2020.

  1. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    Code (CSharp):
    1.  
    2. public GameObject[] gos;
    3. private void Start()
    4.   {
    5. gos = GameObject.FindGameObjectsWithTag("Player");
    6.   }
    7.  
    8. public void Movement()
    9. {
    10.       Vector3 direction = gos[0].transform.localPosition - transform.localPosition;
    11.       Debug.Log("Side: " + direction.x);
    12.  
    13.        if (direction.x > 0 && anim.GetBool("InCombatt") == true)
    14.             {
    15.                sprite.flipX = true;
    16.             }
    17.        else if (direction.x < 0 && anim.GetBool("InCombatt") == true)
    18.             {
    19.               sprite.flipX = false;
    20.             }
    21.  
    22.        Vector3 directio = gos[1].transform.localPosition - transform.localPosition;
    23.        Debug.Log("Side: " + directio.x);
    24.        if (directio.x > 0 && anim.GetBool("InCombatt") == true)
    25.             {
    26.               sprite.flipX = true;
    27.             }
    28.               else if (directio.x < 0 && anim.GetBool("InCombatt") == true)
    29.            {
    30.              sprite.flipX = false;
    31.            }
    32.  
    33.        void Dial()
    34.        {
    35.          float distance = Vector3.Distance(transform.localPosition, gos[0].transform.localPosition);
    36.            // Debug.Log("Distance: " + distance);
    37.            if (distance > 4.0f)
    38.           {
    39.            isHit = false;
    40.            anim.SetBool("InCombat", false);
    41.           }
    42.        }
    43.       void Diatt()
    44.       {
    45.          float distanc = Vector3.Distance(transform.localPosition, gos[1].transform.localPosition);
    46.  
    47.          // Debug.Log("Distance: " + distanc);
    48.          if (distanc > 4.0f)
    49.          {
    50.            isHit = false;
    51.            anim.SetBool("InCombat", false);
    52.          }
    53.       }
    54.  
    55.  
    56.          if (direction.x * direction.x > directio.x * directio.x)
    57.          {
    58.              Diatt();
    59.          }else { Dial(); }
    60. }
    61.  

    this is my problem ,i use array to calculate both distances and directions between two player and one enemy,but when enemy is InCombat enemy consider correct distance but flip to the other side ,because i think it consider the direction of other player,help,( for one player works)
     
    Last edited: Jan 13, 2020
  2. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
  3. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Does you code really look like that in Visual Studio?
     
  4. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Here, a video about convention and syntax in scripting. The part about indentation is at 2:30:

     
  5. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    Maybe more information about the game would help. 2 players and 1 enemy? Is the posted script attached to the enemy? Also the posted script seems to be incomplete or when are the methods called, missing update etc?
     
  6. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    the code is correct now, yes there are two player and one zombie ,the zombie should combat with both enemies but do not work,(read previous about flip and…) the script posted is attached to enemy
     
  7. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    If you say your script compiles then check your posted script again. There are many undeclared variables, methods inside method. Also there is no information when and how is the Movement() called. Try to post your script again by copying it directly from your script editor and give some more information about what is happening and what are you trying to achieve. At this point it is really difficult for someone to help you out based on the information you have given.
     
  8. michele2000bon

    michele2000bon

    Joined:
    Nov 29, 2019
    Posts:
    40
    i do not know what you need,this is all….Movement is called like in the script….i need a way to do not consider direction or directio ,in this way enemy flip correctly,when direction is more near than directio ,do not consider directio and the same for the opposite