Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Move : MonoBehaviour
  6. {
  7. public float speed = 0.4f;
  8. Vector2 dest = Vector2.zero;
  9.  
  10. // Use this for initialization
  11. void Start () {
  12. dest = transform.position;
  13. }
  14.  
  15. // Update is called once per frame
  16. void FiexedUpdate ()
  17. {
  18. // Move closer to Destination
  19. Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
  20. GetComponent<Rigidbody2D>().MovePosition(p);
  21.  
  22. // Check for Input if not moving
  23. if ((Vector2)transform.position == dest)
  24. {
  25. if (Input.GetKey(KeyCode.W) && valid(Vector2.up))
  26. dest = (Vector2)transform.position + Vector2.up;
  27. if (Input.GetKey(KeyCode.D) && valid(Vector2.right))
  28. dest = (Vector2)transform.position + Vector2.right;
  29. if (Input.GetKey(KeyCode.S) && valid(-Vector2.up))
  30. dest = (Vector2)transform.position - Vector2.up;
  31. if (Input.GetKey(KeyCode.A) && valid(-Vector2.right))
  32. dest = (Vector2)transform.position - Vector2.right;
  33. }
  34.  
  35. // Animation Parameters
  36. Vector2 dir = dest - (Vector2)transform.position;
  37. GetComponent<Animator>().SetFloat("DirX", dir.x);
  38. GetComponent<Animator>().SetFloat("DirY", dir.y);
  39. }
  40.  
  41. bool valid(Vector2 dir)
  42. {
  43. // Cast Line from 'next to Pac-Man' to 'Pac-Man'
  44. Vector2 pos = transform.position;
  45. RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
  46. return (hit.collider == GetComponent<Collider2D>());
  47. }
  48. }
  49. Any State to right with the Condition DirX > 0.1
  50. Any State to left with the Condition DirX < -0.1
  51. Any State to up with the Condition DirY > 0.1
  52. Any State to down with the Condition DirY < -0.1
  53. map size : 8 :: pivit bottem left
  54. pacman szie :8
  55.  
  56. The trick is to choose the Collider's Center and Size properties so that they are always like 1.25 or 1.5 or 1.75 or 2.00, and never like 1.24687 or 1.25788. Here are some examples:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement