Search Unity

Texture flip help

Discussion in 'Scripting' started by Rutenis, Jan 22, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Hey guys, im making 2d game. So in this script zombie is following me, but whenever i jump over him he doesnt flip texture. Maybe you know how do i make so he would flip texture?

    Thanks!


    using UnityEngine;
    using System.Collections;

    using UnityEngine;
    using System.Collections;

    public class EnemyAi : MonoBehaviour {
    public Transform target;
    public float moveSpeed;
    public int rotationSpeed;
    public int maxDistance;

    public float X;

    public float speed;

    private Transform myTransform;

    void Awake()
    {
    myTransform = transform;
    }

    // Use this for initialization
    void Start () {
    X = transform.localScale.x;

    GameObject go = GameObject.FindGameObjectWithTag("Player");

    target = go.transform;

    maxDistance = 2;
    }




    // Update is called once per frame
    void FixedUpdate () {
    Debug.DrawLine(target.position, myTransform.position, Color.yellow);



    //Move towards target

    myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // will move in relation to the right of the transform

    myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // will move left in relation to the transform }

    }
    void Update()
    {



    if (target.position.x < myTransform.position.x) myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // player is left of enemy, move left
    if (target.position.x > myTransform.position.x) myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right }


    }


    }
     
  2. AngryGenius

    AngryGenius

    Joined:
    Jan 21, 2014
    Posts:
    14
    Hello! This should do the trick! It has one scale based mode(beware! will break batching) and 2 rotate modes. The paper style rotate is smoothed, like Paper Mario, and the other is set instantly. One problem with the paper mode it that you'll either have to lock your z axis, or convert to global coordinated instead of local for movement. E.G. use Vector3.right instead of transform.right.

    Code (csharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class EnemyAi : MonoBehaviour
    8. {
    9.     public Transform target;
    10.     public float moveSpeed;
    11.     public int rotationSpeed;
    12.     public int maxDistance;
    13.  
    14.     public float X;
    15.  
    16.     public float speed;
    17.  
    18.     private Transform myTransform;
    19.  
    20.     public bool paperStyle;
    21.  
    22.     public bool scaleStyle;
    23.  
    24.     void Awake()
    25.     {
    26.         myTransform = transform;
    27.     }
    28.  
    29.     // Use this for initialization
    30.     void Start()
    31.     {
    32.         X = transform.localScale.x;
    33.  
    34.         GameObject go = GameObject.FindGameObjectWithTag("Player");
    35.  
    36.         target = go.transform;
    37.  
    38.         maxDistance = 2;
    39.     }
    40.  
    41.  
    42.  
    43.  
    44.     // Update is called once per frame
    45.     void FixedUpdate()
    46.     {
    47.         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
    48.  
    49.  
    50.  
    51.         //Move towards target
    52.  
    53.         //myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // will move in relation to the right of the transform
    54.  
    55.         //myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // will move left in relation to the transform    }
    56.  
    57.     }
    58.     void Update()
    59.     {
    60.         Vector3 oldEulers = myTransform.eulerAngles;
    61.  
    62.  
    63.         if (target.position.x < myTransform.position.x)
    64.         {
    65.            
    66.             myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;
    67.  
    68.             //We're checking inside a threshold so the object doesn't rapidly flip when even with the player.
    69.             if (target.position.x < myTransform.position.x-0.02f)
    70.             {
    71.                 if (paperStyle)
    72.                 {
    73.                     oldEulers.y = Mathf.LerpAngle(oldEulers.y, 0, 0.25f);
    74.                 }
    75.                 else if (scaleStyle)
    76.                 {
    77.                     transform.localScale = new Vector3(-1, 1, 1);
    78.                 }
    79.                 else
    80.                 {
    81.                     oldEulers.y = 0;
    82.                 }
    83.             }
    84.         }// player is left of enemy, move left
    85.  
    86.         if (target.position.x > myTransform.position.x)
    87.         {
    88.             //If we're using either rotate style, transform.right will be relative.
    89.             if (scaleStyle  !paperStyle)
    90.             {
    91.                 myTransform.position += myTransform.right * moveSpeed * Time.deltaTime;
    92.             }
    93.             else
    94.             {
    95.                 myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;
    96.             }
    97.  
    98.             //We're checking inside a threshold so the object doesn't rapidly flip when even with the player.
    99.             if (target.position.x > myTransform.position.x + 0.02f)
    100.             {
    101.                 if (paperStyle)
    102.                 {
    103.                     oldEulers.y = Mathf.LerpAngle(oldEulers.y, 180, 0.25f);
    104.                 }
    105.                 else if (scaleStyle)
    106.                 {
    107.                     transform.localScale = new Vector3(1, 1, 1);
    108.                 }
    109.                 else
    110.                 {
    111.                     oldEulers.y = 180;
    112.                 }
    113.             }
    114.         }// player is right of enemy, move right   
    115.  
    116.         transform.eulerAngles = oldEulers;
    117.  
    118.  
    119.     }
    120.  
    121.  
    122. }
    Hope it was helpful! :D
     
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Wow, it works! Youre good at scripting dude! Thanks a lot!