Guest User

Untitled

a guest
Oct 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyAI : MonoBehaviour {
  5. public float walkSpeed = 1.0f; // Walkspeed
  6. public float wallLeft = 0.0f; // Define wallLeft
  7. public float wallRight = 5.0f; // Define wallRight
  8. float walkingDirection = 1.0f;
  9. Vector2 walkAmount;
  10. float originalX; // Original float value
  11.  
  12.  
  13. void Start () {
  14. this.originalX = this.transform.position.x;
  15. }
  16.  
  17. // Update is called once per frame
  18. void Update () {
  19. walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
  20. if (walkingDirection > 0.0f && transform.position.x >= wallRight) {
  21. walkingDirection = -1.0f;
  22. } else if (walkingDirection < 0.0f && transform.position.x <= wallLeft) {
  23. walkingDirection = 1.0f;
  24. }
  25. transform.Translate(walkAmount);
  26. }
  27. }
  28.  
  29. void Start () {
  30. wallLeft = transform.position.x - 2.5f;
  31. wallRight = transform.position.x + 2.5f;
  32. }
  33.  
  34. void Update () {
  35. walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
  36. if (walkingDirection > 0.0f && transform.position.x >= originalX + wallRight) {
  37. walkingDirection = -1.0f;
  38. } else if (walkingDirection < 0.0f && transform.position.x <= originalX - wallLeft) {
  39. walkingDirection = 1.0f;
  40. }
  41. transform.Translate(walkAmount);
  42. }
Add Comment
Please, Sign In to add comment