Home c# Unity (C #) - Why do not borrows work?

Unity (C #) – Why do not borrows work?

Author

Date

Category

recently began to study coatines. I do not quite understand how they work. I wrote a boss script for the game, and used coatines (for the first time). I needed that, when moving to the second stage, the boss was moving along the scheme from Corutin, but when moving to the second stage, he continues movement just like on the first. Here is the code:

using system.collections;
Using System.Collections.Genic;
using unityengine;
Using System.Collections;
Public Class Boss2: Monster
{
[SerializeField]
Private Float Speed ​​= 2.0F; // Boss Movement Speed
[SerializeField]
Public Float Posmax1; // Maximum point to which the boss
[SerializeField]
Public Float Posmin1; // the minimum point to which the boss
[SerializeField]
Public Float Posmax2; // Second maximum point to which the boss can reach
[SerializeField]
Public Float Posmin2; // Second minimum point to which the boss can reach
[SerializeField]
int hitpoints = 7; // HP Boss (Health)
[SerializeField]
int stadium = 1; // Stage Boss
Vector3 Direction;
Private Spriterenderer Sprite;
PRIVATE BULLET BULLET;
Ienumerator Waiting () // Create a corounte, which we will use others to create a delay of 5 seconds
{
  Yield Return New WaitForSeconds (5); // Delay in 5 seconds for future corutin
}
Ienumerator start1 () // Create a corounte, with which, then we will lower the boss for 10 down
{
  transform.position + = vector3.down * 10 * mathf.sin (time.deltatime); // lower boss for 10 down
  Debug.log ("complite"); // Checking whether Korutina was executed
}
Ienumerator start2 () // Create a corounte, with the help of which, then we will move the boss to the right, to the left for 5 seconds
{
  Yield Return StartCoroutine (Waiting ());
  if (transform.position.x & gt; = posmax2)
  {
    direction * = -1.0f;
  }
  ELSE If (transform.position.x & lt; = posmin2)
  {
    direction = transform.right;
  }
  Debug.log ("complite"); // Checking whether Korutina was executed
}
Ienumerator start3 () // Create a coast with which, then we will raise the boss for 10 up
{
  Yield Return StartCoroutine (Waiting ());
  transform.position + = vector3.up * 10 * mathf.sin (time.deltatime);
  Debug.log ("complite"); // Checking whether Korutina was executed
}
Ienumerator start4 () // Create a coast with which, then we will move the boss to the right, left for 5 seconds
{
  Yield Return StartCoroutine (Waiting ());
  if (transform.position.x & gt; = posmax1)
  {
    direction * = -1.0f;
  }
  ELSE if (transform.position.x & lt; = posmin1)
  {
    direction = transform.right;
  }
  Debug.log ("complite"); // Checking whether Korutina was executed
}
Void Start5 () // Create a method, thanks to which, then continue the movement of the boss, as in the first stage without any delays in 5 seconds
{
  if (transform.position.x & gt; = posmax1)
  {
    direction * = -1.0f;
  }
  ELSE if (transform.position.x & lt; = posmin1)
  {
    direction = transform.right;
  }
}
PROTECTED OVERRIDE VOID AWAKE ()
{
  Sprite = GetComponentinchildren & LT; Spriterender & GT; ();
  Bullet = Resources.load & lt; Bullet & gt; ("Bullet");
}
PROTECTED OVERRIDE VOID START ()
{
  direction = transform.right; // The initial direction of the boss - the right
  Stadium = 1; // Initial Boss Stage - 1
}
Void Update ()
{
  Move ();
}
Public Void Damaged () // Creating the logic of reducing the HP boss + logic of the transition to the second stage
{
  HitPoints--; // Take the Health
  if (Stadium == 1 & amp; & amp; hitpoints == 0) // If the boss is in the first stage, and its HP has become 0, go to the second stage and regenerating health
  {
    Stadium = 2;
    HitPoints = 10;
  }
  ELSE If (Stadium == 2 & amp; & amp; hitpoints == 0) // If the boss is in the second stage, and its HP has become 0, we destroy the boss
  {
    Destroy (GameObject);
  }
}
PROTECTED VIRTUAL VOID ONTRIGGERENTER2D (COLLIDER2D COLLIDER)
{
  Bullet Bullet = Collider.getcomponent & lt; Bullet & gt; ();
  if (bullet) // If the boss got a bullet - we take him a hp
  {
    Damaged ();
  } 
Character Character = Collider.getcomponent & lt; Character & GT; ();
  if (character) // If the boss hit the player - take away the health of the player
  {
    Character.ReceivedAMage ();
  }
}
Private Void Move () // Logic Movement Boss
{
  if (Stadium == 1) // If the boss is in the first stage - move it to the right, left until it goes to the second stage or will not kill the player
  {
    if (transform.position.x & gt; = posmax1)
    {
      direction * = -1.0f;
    }
    ELSE if (transform.position.x & lt; = posmin1)
    {
      direction = transform.right;
    }
  }
  Else if (Stadium == 2) // If the boss moved to the second stage - we move the infinitely boss on the script you need (move right, left = & gt; descent down = & gt; move right, left = & gt; raise upstairs)
  {
    Start5 (); // continue the movement of the boss as in the first stage, without delay
    While (True) // In order for the Movement of the boss on a specified script repeated, it turns this thing to the infinite cycle.
    {
    StartCoroutine (start1 ()); // Start the first Corutin - We are waiting for 5 seconds, then lower the boss for 10 down
      StartCoroutine (start2 ()); // Start the second corutin - moving the boss from point to point within 5 seconds (in the beginning you can throw out 5 seconds)
    STARTCOROUTINE (START3 ()); // Start the third Corutin - Raise the boss upstairs to 10
    StartCoroutine (start4 ()); // Let's start the fourth corutin - move the boss from the point to the point within 5 seconds
    }
    // From this there should be an endless movement of the boss for the direction we specify (script)
  }
  transform.position = vector3.movetowards (transform.position, transform.position + Direction, Speed ​​* Time.deltatime);
  Sprite.FlipX = direction.x & gt; 0.0F;
}

}

I will be glad to your help.
P.S. I am a beginner, and this is my first project


Answer 1

you noticed that Start5 you have a regular method, and not a koruto, right?

It is difficult, of course, to understand the magic that is happening there, so I don’t touch the code, but I will help in the essence of the syntax: all the coruts should be run through StartCoroutine (), and you have a simple method call – therefore it does not work.

And the movement, as in the first stage you have because the first stage and the Start5 method contain the same code. Because The START5 method is not a coruth, then it is the only one of your challenges in the second stage and, as a result, you do not notice the difference in motion.

Here you have Article on Habré , where more less details about Korutina is painted. Perhaps a little help in awareness 🙂

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions