Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2d zombie ai, texture flip. Need help.

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

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Hey guys, this script down here is what i use to zombie to folow the player. But i cant make zombie flip texture whenever i jump around him. I marked the problem. I suck at scripting :D


    I get error: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

    public Transform target;
    public float moveSpeed;
    public int rotationSpeed;
    public int maxDistance;

    public float X;

    public bool facingRight = true;


    private Transform myTransform;

    void Awake() {
    myTransform = transform;
    }

    // Use this for initialization
    void Start () {
    X = myTransform.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
    myTransform.position.x = -X;
    if (target.position.x > myTransform.position.x) myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right }
    myTransform.position.x = X;


    }


    }
     
    Last edited: Jan 19, 2014
  2. Ilkzz

    Ilkzz

    Joined:
    Feb 22, 2013
    Posts:
    16
    Hey,

    Did you get the solution to this? I also need help with enemy facing player