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

flip sprite while using mathf.sin

Discussion in '2D' started by DomDomDom, Oct 18, 2015.

  1. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Hi there,

    Below is my code I'm using to move an object (enemy in this case) back and forth. However I'm struggling to figure out my logic (in the fixed update function) to try and flip the sprite once it moves back and forth.

    Code (CSharp):
    1. using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class EnemyAI : MonoBehaviour {
    5.         [SerializeField] float amplitude = 1.0f;
    6.         [SerializeField] float speed = 1.0f;
    7.         [SerializeField] Axis axis = Axis.XAxis;
    8.      
    9.         Vector3 origin = Vector3.zero;
    10.         float t = 0.0f;
    11.  
    12.         private bool facingRight = true;
    13.         Transform enemyGraphics;
    14.  
    15.         Vector3 position;
    16.  
    17.  
    18.         void Start() {
    19.             enemyGraphics = transform.FindChild ("Graphics");
    20.             if (enemyGraphics == null) {
    21.                 Debug.LogError("There is no 'Graphics' object as a child of the Enemy!");          
    22.             }
    23.             this.origin = this.transform.position;
    24.         }
    25.      
    26.         void Update() {
    27.             t = Time.realtimeSinceStartup;
    28.          
    29.             position = this.transform.position;
    30.  
    31.             switch(this.axis) {
    32.             case Axis.XAxis: {
    33.              
    34.                 position.x = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.x;
    35.  
    36.                 this.transform.position = position;
    37.  
    38.             } break;
    39.             case Axis.YAxis: {
    40.              
    41.                 position.y = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.y;
    42.              
    43.                 this.transform.position = position;
    44.              
    45.             } break;
    46.             case Axis.ZAxis: {
    47.                 position.z = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.z;
    48.              
    49.                 this.transform.position = position;
    50.              
    51.             }break;
    52.             }
    53.         }
    54.  
    55.      
    56.         void FixedUpdate () {
    57.             //Checking to see what direction our enemy is going so and to see what way we need to flip it
    58.             // If the input is moving the enemy right and the enemy is facing left...
    59.             if (this.origin.x > this.origin.x && !facingRight)
    60.                 // ... flip the enemy.
    61.                 Flip();
    62.             // Otherwise if the input is moving the enemy left and the enemy is facing right...
    63.             else if (this.origin.x < this.origin.x && facingRight)
    64.                 // ... flip the enemy.
    65.                 Flip();
    66.         }
    67.      
    68.         private void Flip()
    69.         {
    70.             // Switch the way the enemy is labelled as facing.
    71.             facingRight = !facingRight;
    72.          
    73.             //Multiply the enemy's x local scale by -1.
    74.             Vector3 theScale = enemyGraphics.localScale;
    75.             theScale.x *= -1;
    76.             enemyGraphics.localScale = theScale;
    77.         }
    78.  
    79.     }
    80.  
    81.  
    82.     public enum Axis {
    83.         XAxis,
    84.         YAxis,
    85.         ZAxis
    86.     }
    Thanks for your help in advance.
     
  2. IntDev

    IntDev

    Joined:
    Jan 14, 2013
    Posts:
    152
    Code (CSharp):
    1. this.origin.x > this.origin.x
    this will never happen
     
  3. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    I know. That is the part I'm struggling with as far as logic.
     
  4. IntDev

    IntDev

    Joined:
    Jan 14, 2013
    Posts:
    152
    Maybe you want something like this

    Code (CSharp):
    1. this.origin.x > this.transform.position.x
     
  5. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Getting better. At the origin of the enemy it flips in the center rather than the end on the left or right side.
     
  6. VorpalSilence

    VorpalSilence

    Joined:
    Oct 22, 2015
    Posts:
    13
    You want to check what its position will be in the next frame and flip it if it will be travelling the other way.
    Would probably want to be something like:

    Code (csharp):
    1.  
    2. float NextXPos = this.amplitude * Mathf.Sin((Time.realtimeSinceStartup + 0.1)* this.speed) + this.origin.x;
    3.  
    4. if (NextXPos > this.transform.position.x && ! facingRight)
    5.  
    Might need to increase or decrease that + 0.1 depending on how it looks in your scene.
     
  7. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    After some trial and error, I got it working. Thank you!