Advertisement
Guest User

Raycast Problems

a guest
Aug 29th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CrossyPlayer : MonoBehaviour {
  5.  
  6.     float amount = 0.70f;
  7.  
  8.  
  9.     //RayCasting things
  10.     float lenghtRayX;
  11.     float lenghtRayY;
  12.     Vector2 origin;
  13.  
  14.     //CollisionThings
  15.     bool collidingUp = false;
  16.     bool collidingDown = false;
  17.     bool collidingRight = false;
  18.     bool collidingLeft = false;
  19.  
  20.     // Use this for initialization
  21.     void Start () {
  22.         lenghtRayX = GetComponent<Collider2D>().bounds.extents.x * 2;
  23.         lenghtRayY = GetComponent<Collider2D>().bounds.extents.y * 2;
  24.         origin = transform.position;
  25.     }
  26.    
  27.     // Update is called once per frame
  28.     void Update () {
  29.         IsCollidingDown();
  30.         IsCollidingUp();
  31.         if ((Input.GetKeyDown("up") || Input.GetKeyDown(KeyCode.W)) && !collidingUp )
  32.         {
  33.             transform.position = new Vector2(transform.position.x, transform.position.y + amount); ;
  34.         }
  35.         if ((Input.GetKeyDown("down") || Input.GetKeyDown(KeyCode.S)) && !collidingDown)
  36.         {
  37.             transform.position = new Vector2(transform.position.x, transform.position.y - amount); ;
  38.         }
  39.         if (Input.GetKeyDown("left") || Input.GetKeyDown(KeyCode.A))
  40.         {
  41.             transform.position = new Vector2(transform.position.x - amount, transform.position.y); ;
  42.         }
  43.         if (Input.GetKeyDown("right") || Input.GetKeyDown(KeyCode.D))
  44.         {
  45.             transform.position = new Vector2(transform.position.x + amount, transform.position.y ); ;
  46.         }
  47.     }
  48.  
  49.  
  50.     void IsCollidingUp()
  51.     {
  52.         origin = transform.position;
  53.         Vector2 direction = Vector2.up ;
  54.         Ray2D ray = new Ray2D(origin, direction);
  55.         Debug.DrawRay(ray.origin, ray.direction, Color.yellow);
  56.         RaycastHit2D hit = Physics2D.Raycast(origin, direction);
  57.         if (hit.collider.tag == "Bounds")
  58.         {
  59.             Debug.Log("Collision");
  60.             collidingUp = true;
  61.         }
  62.  
  63.         collidingUp = false;
  64.     }
  65.  
  66.     void IsCollidingDown()
  67.     {
  68.         origin = transform.position;
  69.         Vector2 direction = Vector2.down;
  70.         Ray2D ray = new Ray2D(origin, direction);
  71.         Debug.DrawRay(origin, direction, Color.yellow);
  72.         RaycastHit2D hit = Physics2D.Raycast(origin,direction);
  73.         if (hit.collider.tag == "Bounds")
  74.         {
  75.             Debug.Log("Collision");
  76.             collidingDown = true;
  77.         }
  78.  
  79.         collidingDown = false;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement