using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveWolf : MonoBehaviour { public Vector3 MovementVector = new Vector3(0.1f, 0, 0); public int DirectionMultiplier = 1; SpriteRenderer _mySpriteRenderer; /* The flipX property of the SpriteRenderer Component makes Wolf face left/right. * This line of code creates a field, named _mySpriteRenderer, that will (eventually) hold a reference to the GameObject's * SpriteRenderer component. * Initially, this field contains the "null" value. It doesn't yet "point to" the desired GameObject. */ // Start is called before the first frame update void Start() { _mySpriteRenderer = GetComponent(); // Point _mySpriteRenderer to the SpriteRenderer Component of the attached GameObject. } // Update is called once per frame void Update() { if(transform.position.x > 10.0f || transform.position.x < -10.0f) // if the GameObject moves past the right or left edge of screen ... { _mySpriteRenderer.flipX = !_mySpriteRenderer.flipX; // face the GameObject the opposite way DirectionMultiplier *= -1; // change the movement direction } transform.position += MovementVector * DirectionMultiplier; } }