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 } } }
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): 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; public bool paperStyle; public bool scaleStyle; 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() { Vector3 oldEulers = myTransform.eulerAngles; if (target.position.x < myTransform.position.x) { myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; //We're checking inside a threshold so the object doesn't rapidly flip when even with the player. if (target.position.x < myTransform.position.x-0.02f) { if (paperStyle) { oldEulers.y = Mathf.LerpAngle(oldEulers.y, 0, 0.25f); } else if (scaleStyle) { transform.localScale = new Vector3(-1, 1, 1); } else { oldEulers.y = 0; } } }// player is left of enemy, move left if (target.position.x > myTransform.position.x) { //If we're using either rotate style, transform.right will be relative. if (scaleStyle !paperStyle) { myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; } else { myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; } //We're checking inside a threshold so the object doesn't rapidly flip when even with the player. if (target.position.x > myTransform.position.x + 0.02f) { if (paperStyle) { oldEulers.y = Mathf.LerpAngle(oldEulers.y, 180, 0.25f); } else if (scaleStyle) { transform.localScale = new Vector3(1, 1, 1); } else { oldEulers.y = 180; } } }// player is right of enemy, move right transform.eulerAngles = oldEulers; } } Hope it was helpful!