Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Laser : MonoBehaviour {
  6.  
  7. public GameObject prefab;
  8. public float laserPower;
  9. // Use this for initialization
  10. void Start () {
  11.  
  12. }
  13.  
  14. // Update is called once per frame
  15. void Update () {
  16. //we use rays to project invisible lines at a distance
  17. //in this case, we are using the ScreenPointToRay function of our main camera
  18. //to convert the mouse position on the screen into a direction projected through the screen
  19. //(in class, i drew a dinosaur on the other side)
  20. Ray beam = Camera.main.ScreenPointToRay(Input.mousePosition);
  21.  
  22. //draw our debug ray so we can see inside unity
  23. //the ray starts from beam.origin and is drawn to 1000 units in the rays direction (beam.direction)
  24. Debug.DrawRay(beam.origin, beam.direction * 1000f, Color.red);
  25.  
  26. //declare and initialize our raycasthit to store hit information
  27. RaycastHit beamHit = new RaycastHit();
  28.  
  29. //this both casts the ray "beam" and returns true if the ray hits any collider
  30. //the second parameter is where our raycasthit information is stored
  31. //the third parameter is how far to cast the ray
  32. if (Physics.Raycast(beam, out beamHit, 1000f)){
  33.  
  34. //if the raycast hits a rigidbody and the player is pressing the right mouse button
  35. if (beamHit.rigidbody && Input.GetMouseButton(0)){
  36. //we use insideunitsphere to get a random 3D direction and multiply the direction by our power variable
  37. beamHit.rigidbody.AddForce(Random.insideUnitSphere * laserPower);
  38. }
  39.  
  40. //if the raycast hits something and the player is pressing the right mouse button
  41. if (Input.GetMouseButton(1)){
  42. //Instantiate(prefab, beamHit.point, Quaternion.identity);
  43. //Debug.Log(beamHit.point);
  44. }
  45. //if the raycast hits something and the player is pressing the middle mouse button
  46. if (Input.GetMouseButton(2)){
  47. //Destroy this will delete this script from the object its attached to
  48. //Destroy(this);
  49.  
  50. //we can also destroy a given gameobject
  51. //using our raycasthit beamhit to access the transform of the thing hit and its parent gameobject
  52. //Destroy(beamHit.transform.gameObject);
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement