If Statement and Sequences
« on: June 26, 2015, 11:16:15 AM »
Hello,

I am setting up a simple enemy ai script using DoTween.

I have 4 trigger points on the enemy. Depending on which trigger is triggered the enemy will move and rotate a certain direction.

My question is when I have more than one IF statement in Update does it wait untill completion of a DOMove Sequence(after the enemy has moved to the designated position) to recheck if any of the other "IF" Statements are true. Or will Update check to see if any "If" statements are true even before the completion of the Sequenced DOMove designated position has been reached. My code is not final or tested and written for this question only so far.

My movement is similar to grid system movement as I want all end positions to land on an Interger so my concern would be that another If statement would interupt the completion of the enemies movement thus the new movment would begin too soon.


Sample Code:

 void Update()
   {
   
       //forward
   if (oneTrigger && twoTrigger.colliding)
       
       Sequence mySequence = DOTween.Sequence();
      mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative());
       
        //Down
       else if (!oneTrigger && !twoTrigger.colliding)
       
        // Set the new target Rotation and new target Move
       Sequence mySequence = DOTween.Sequence();
        mySequence.Append(transform.DORotate(new Vector3(0,90,0), 1));
   mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative());

       //Turn
       else if (oneTrigger && threeTrigger.colliding)
       
        // Set the new target Rotation and new target Move
       Sequence mySequence = DOTween.Sequence();
        mySequence.Append(transform.DORotate(new Vector3(0,180,0), 1));
   mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative());
       
       
      {

As a safeguard againts moving to soon I might add something like this to see if the enemy is positioned on a Interger:
   
     public int myX = (int)transform.position.x;
     public int myZ = (int)transform.position.z;

 void Update()
   {
   if (!oneTrigger && !twoTrigger.colliding && myX==int && myZ==int)

        Sequence mySequence = DOTween.Sequence();
      mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative());

//I am not sure if this add on of the Int would be necessary?

Thanks
R_Chevallier
« Last Edit: June 26, 2015, 11:26:59 AM by r_chevallier »

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: If Statement and Sequences
« Reply #1 on: June 26, 2015, 11:25:28 AM »
Hi,


Update is a method that is automatically called every single frame in Unity. That means that your IFs will be checked and executed every frame, regardless of whether a tween/sequence/anything-else is running or not.


Cheers,
Daniele

Re: If Statement and Sequences
« Reply #2 on: June 26, 2015, 11:34:08 AM »
I modified my question near the end. What do you think of my attemp of checking if the enemy is positioned on an Interger. If this works this would prevent any premature movement until DOMove has landed the enemy on a whole number. I am moving 1 unit at a time.

Thanks,
R_Chevallier

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: If Statement and Sequences
« Reply #3 on: June 26, 2015, 12:07:07 PM »
If your whole tween moves by one integer, and you just want to check if it's complete, it would be better if you stored the Sequence as a class variable, then check if it's complete or not (via IsComplete):
Code: [Select]
if (mySequence.IsComplete()) {
   // sequence complete
}

Re: If Statement and Sequences
« Reply #4 on: June 29, 2015, 11:24:35 AM »
Also here's a general optimisation tip that may help you - if certain code only needs to be run for the duration of certain trigger states, then you might consider reading up on coroutines as a more efficient approach than using Update for everything and consuming cycles even when none of the IFs are firing.  Some useful links:

http://docs.unity3d.com/Manual/Coroutines.html
http://twistedoakstudios.com/blog/Post83_coroutines-more-than-you-want-to-know
http://unitypatterns.com/introduction-to-coroutines/

RE: bool isComplete()
« Reply #5 on: July 07, 2015, 11:55:39 AM »
Hello,

Here is a more revised script. I am trying to make sure sequence is complete before allowing enemy to move. I have set up my conditions in update and am using a utility class script to hold collision information. This part works but this is my problem: My enemy character moves almost 3 units at a time when I only want him to move one unit when the right collisions occur. I am trying to set up "bool isComplete" to get the movement right but am having trouble.

I will eventually have more move directions in place but for now I just have move Forward. Please advise how and where to paste the proper DOTween  "bool isComplete" script.

I do have "bool isComplete = mySequence.IsComplete();" in Start. I am not sure if this is the proper location.

Thanks in advance.

Here is my script:

public class EnemyMove : MonoBehaviour {
   
   //public bool isComplete = true;
   private bool isMoving = false;
   private TriggerParent leftTrigger;
   private TriggerParent frontDownTrigger;
   private TriggerParent frontTrigger;
   private Sequence mySequence;

   internal bool rotatePosDown = false;

   public GameObject frontBounds;                  //trigger for sight bounds
   public GameObject frontDownBounds;      



   void Awake()
   {      

   
      if(frontBounds)
      {
         frontTrigger = frontBounds.GetComponent<TriggerParent>();
         if(!frontTrigger)
            Debug.LogError("'TriggerParent' script needs attaching to enemy 'SightBounds'", frontBounds);
      }
      if(!frontBounds)
         Debug.LogWarning("Assign a trigger with 'TriggerParent' script attached, to 'SightBounds' or enemy will not be able to see", transform);
      
      if(frontDownBounds)
      {
         frontDownTrigger = frontDownBounds.GetComponent<TriggerParent>();
         if(!frontDownTrigger)
            Debug.LogError("'TriggerParent' script needs attaching to enemy 'attackBounds'", frontDownBounds);
      }
      else
         Debug.LogWarning("Assign a trigger with 'TriggerParent' script attached, to 'AttackBounds' or enemy will not be able to attack", transform);
   }

   // Use this for initialization
   void Start () {
      Sequence mySequence = DOTween.Sequence();
      bool isComplete = mySequence.IsComplete();
   }


   void Update() {

      //Moving forward
      if (frontTrigger && !frontTrigger.colliding && frontDownTrigger && frontDownTrigger.colliding)
      {

         // Move one step forward
         Move("forward");

         
      }
   }

void Move(string moveDirection)
{
   if( isMoving == false)
   {
      // The object is moving
      //isMoving = true;
      
      switch( moveDirection.ToLower() )
      {
         
      case "forward":
            
            print("foward");
            mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative());
            

            break;
            
      case "right":
            
            mySequence.Append(transform.DORotate(new Vector3(0, 90, 0), 0.25f).SetRelative());
         
            break;

         }
      }
   }
}


Thanks again.

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: If Statement and Sequences
« Reply #6 on: July 07, 2015, 06:30:28 PM »
But why not use an OnComplete callback to set/know/react to a tween's completion?

Re: Sequences and SetEase
« Reply #7 on: July 09, 2015, 12:05:40 PM »
Hello,

Getting closer to my code working properly.

This is a basic question I'm sure but I cant get it right.

I am trying to SetEase but I am getting an error. What is the right way to write this line of code.

mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative.SetEase(Ease.Linear));


Thank You.

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: If Statement and Sequences
« Reply #8 on: July 09, 2015, 04:30:04 PM »
You forgot the parenthesis after SetRelative, that's why you're getting an error. This will work:

Code: [Select]
mySequence.Append(transform.DOMove(new Vector3(1,0,0), 0.5f).SetRelative().SetEase(Ease.Linear));