Raycasthit2d



Mar 20th, 2015
Raycasthit2d array
Never
Api
Not a member of Pastebin yet?Sign Up, it unlocks many cool features!
  1. usingSystem.Collections;
  2. publicclass Gun2 : MonoBehaviour {
  3. publicfloat fireRate =0;
  4. public LayerMask whatToHit;
  5. public Transform BulletPrefab;
  6. float timeToFire =0;
  7. // Use this for initialization
  8. spawn = transform.FindChild('spawn');
  9. Debug.LogError('No Spawn');
  10. }
  11. // Update is called once per frame
  12. if(fireRate 0){
  13. Shoot ();
  14. }else{
  15. if(Input.GetButtonDown('Shoot')&& Time.time> timeToFire){
  16. Shoot ();
  17. }
  18. }
  19. void Shoot (){
  20. //gets the position of the mouse
  21. PosOfMouse = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z- Camera.main.transform.position.z));
  22. Vector2 spawnPosition =new Vector2 (spawn.position.x, spawn.position.y);
  23. RaycastHit2D hit = Physics2D.Raycast(spawnPosition,PosOfMouse - spawnPosition);
  24. //Effect ();
  25. //Instantiate (BulletPrefab, spawn.position, spawn.rotation) ; //creates the actual bullet
  26. Transform BulletInstance = Instantiate (BulletPrefab, spawn.position, spawn.rotation)as Transform;
  27. Transform MuzzleFlashInstance = Instantiate (MuzzleFlashPrefab, spawn.position, spawn.rotation)as Transform;//Using a Cast, instantiate inside transform variable
  28. MuzzleFlashInstance.parent= spawn;//sets Instance of Muzzle flash to be a child of the spawn of the bullet.
  29. float MuzzleFlashSize = Random.Range(0.4f, 0.9f);//Sets the MuzzleFlash to a random size.
  30. MuzzleFlashInstance.localScale=new Vector3 (MuzzleFlashSize, 0.14f, 0);
  31. Destroy (MuzzleFlashInstance.gameObject, 0.02f);//Destroys the instance of the MuzzleFlashPrefab after use. .gameObject is used to destroy the transform of an object.
  32. Debug.DrawLine(spawnPosition, (PosOfMouse -spawnPosition)*100,Color.yellow);
  33. //when something is actually hit
  34. Debug.Log('Hit '+ hit.collider.name+' and did '+ Damage +' Damage');
  35. }
RAW Paste Data

RaycastHit2D itself is a structure and not a class so hit can't be null; this means you have to check for the collider of a RaycastHit2D variable. PDF - Download unity3d for free. RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero); if (hit.collider!= null). How to make from RayCastHit2D grappling gun / hook in 3 steps? Unity Engine tutorial under 1 minute. Code - https://gist.github.com/FilipCizek/e79f9e0ed07fc. So basically the raycast is for an enemy to check if there is a cliff in front of it. The enemy is knocked back into the air and because of that, the raycast doesn't work properly, so I want to disable it.

RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.back); then the if (hit.collider!= null) block is always true, even if I click on empty space, and it always returns the same coordinate. To illustrate this, here is a video of me clicking the game view. Mac os x for dvd.

Raycasthit2d.collider