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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do I make an Enemy Sprite Change Direction after a Timer <= 0?

Discussion in '2D' started by ryannicholasbecker, Oct 18, 2020.

  1. ryannicholasbecker

    ryannicholasbecker

    Joined:
    Oct 10, 2020
    Posts:
    5
    What I would like for this enemy sprite to do is to move side to side on the x axis until the player destroys said enemy. I have the timer and resetting it figured out but can't quite figure out how to change the directional movement once it does reach 0. I know I need a variable or a bool but all I've been able to do is make it drift in one direction or I get stuck with errors

    Help would be appreciated

    and the code that I have a the moment

    {

    1. public float speed = 3f;
    2. public float resetTimer = 3f;
    3. public float timer = 3f;
    4. void Update (){
    5. timer -= Time.deltaTime;
    6. if (timer <= 0) {
    7. Debug.Log ("turn");
    8. timer = resetTimer;
    9. }
    10. transform.position += Vector3.right * speed * Time.deltaTime;
    11. }
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    The simplest way to do it would be to make your X scale of your object -1. As for moving the opposite direction, you'll have to set a bool variable when the turn happens, and test that bool to decide whether to use Vector3.right or Vector3.left.
     
  3. ryannicholasbecker

    ryannicholasbecker

    Joined:
    Oct 10, 2020
    Posts:
    5
    @Derekloffin

    I should have mentioned this in first post, I am very much a beginner to programming

    Could you sus out what you mean by x scale value -1? I'm not sure I'm understanding that

    For the direction, I set a bool direct = true but now I don't know how to set a vector3 to direct (unless direct can be put in a float with vector3.right/left)

    Code (csharp):
    1.  
    2.     bool direct = true;
    3.     public float speed = 3f;
    4.     public float resetTimer = 3f;
    5.     public float timer = 3f;
    6.  
    7.  
    8.     void Update (){
    9.         timer -= Time.deltaTime;
    10.  
    11.         if (timer <= 0) {
    12.             Debug.Log ("turn");
    13.            
    14.             timer = resetTimer;
    15.  
    16.         }
    17.         if (direct) {
    18.             transform.position += Vector3.right * speed * Time.deltaTime;
    19.         }
    20.     }
    21.  
    22.     void turn(){
    23.         if (timer <= 0 && direct) {
    24.             direct = false;
    25.         } else {
    26.             direct = true;
    27.         }
    28.     }
    29. [CODE]
    30.  
    31. apologies for the code being formatted poorly last time and hope it is more readable this time around
     
    Last edited: Oct 18, 2020
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    They're saying you can set the x-value of the object's Transform.localScale property to a negative number in order to flip its sprite in the other direction. You can then set it back to a positive number to flip back towards its original direction.

    With that in mind, you can simply multiply the x-scale of the object by -1 to flip it back and forth:
    Code (CSharp):
    1. void Flip() {
    2.    Vector3 scale = transform.localScale;
    3.    transform.localScale = new Vector3(scale.x * -1, scale.y, scale.z);
    4. }
    See using code tags for posting readable code on the forums.
     
  5. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  6. ryannicholasbecker

    ryannicholasbecker

    Joined:
    Oct 10, 2020
    Posts:
    5
    Thank you for the help

    this is how it turned out

    Code (csharp):
    1.  
    2.     public float speed = 3f;
    3.     public float resetTimer = 3f;
    4.     public float timer = 3f;
    5.     public float maxRot = 0f;
    6.     Vector3 direction = Vector3.right;
    7.  
    8.     public void Start(){
    9.    
    10.     }
    11.        
    12.     public void Update (){
    13.  
    14.         Vector3 drift = transform.position;
    15.  
    16.         drift += direction * speed * Time.deltaTime;
    17.  
    18.         transform.position = drift;
    19.  
    20.         transform.Rotate (0, 0, maxRot);
    21.  
    22.         if (timer > 0) {
    23.             timer -= Time.deltaTime;
    24.  
    25.         } else {
    26.             Debug.Log ("turn");
    27.  
    28.             timer = resetTimer;
    29.  
    30.             if (direction == Vector3.right) {
    31.  
    32.                 direction = Vector3.left;
    33.            
    34.             } else {
    35.            
    36.                 direction = Vector3.right;
    37.            
    38.             }
    39.        
    40.         }
    41.     }
    42. }