Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Move : MonoBehaviour
- {
- public float speed = 0.4f;
- Vector2 dest = Vector2.zero;
- // Use this for initialization
- void Start () {
- dest = transform.position;
- }
- // Update is called once per frame
- void FiexedUpdate ()
- {
- // Move closer to Destination
- Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
- GetComponent<Rigidbody2D>().MovePosition(p);
- // Check for Input if not moving
- if ((Vector2)transform.position == dest)
- {
- if (Input.GetKey(KeyCode.W) && valid(Vector2.up))
- dest = (Vector2)transform.position + Vector2.up;
- if (Input.GetKey(KeyCode.D) && valid(Vector2.right))
- dest = (Vector2)transform.position + Vector2.right;
- if (Input.GetKey(KeyCode.S) && valid(-Vector2.up))
- dest = (Vector2)transform.position - Vector2.up;
- if (Input.GetKey(KeyCode.A) && valid(-Vector2.right))
- dest = (Vector2)transform.position - Vector2.right;
- }
- // Animation Parameters
- Vector2 dir = dest - (Vector2)transform.position;
- GetComponent<Animator>().SetFloat("DirX", dir.x);
- GetComponent<Animator>().SetFloat("DirY", dir.y);
- }
- bool valid(Vector2 dir)
- {
- // Cast Line from 'next to Pac-Man' to 'Pac-Man'
- Vector2 pos = transform.position;
- RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
- return (hit.collider == GetComponent<Collider2D>());
- }
- }
- Any State to right with the Condition DirX > 0.1
- Any State to left with the Condition DirX < -0.1
- Any State to up with the Condition DirY > 0.1
- Any State to down with the Condition DirY < -0.1
- map size : 8 :: pivit bottem left
- pacman szie :8
- 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