SFE_LaserEffect.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //this script is about how a laser changes during its lifetime (fading, enlarging, etc)
  2. //this is added individually for each line effect, so each line can be parametrised differently
  3. var laser:LineRenderer;
  4. var laserSize:float=0.1;
  5. var fadeSpeed:float=1;
  6. var enlargeSpeed:float=0;
  7. var beginTintAlpha:float=0.5;
  8. var myColor:Color;
  9. private var time:float=0.0;
  10. private var alpha:float=0.0;
  11. var normalizeUV:boolean=false;
  12. var normalizeUvLength:float=1;
  13. var maxRange:float=300;
  14. private var lasBegin:Vector3;
  15. private var lasEnd:Vector3;
  16. function Start()
  17. {
  18. var direction = transform.TransformDirection(Vector3.up);
  19. var hit : RaycastHit;
  20. if (Physics.Raycast (transform.position, direction, hit)) { // if it hits something, we set the laser beam to be that long
  21. laser.SetPosition(0, transform.position);
  22. laser.SetPosition(1, hit.point);
  23. lasBegin=transform.position;
  24. lasEnd=hit.point;
  25. }
  26. else //if the raycast hits nothing, we set the line's endpoint to maxRange
  27. {
  28. laser.SetPosition(0, transform.position);
  29. var endOfLaser:Vector3=transform.TransformDirection(transform.position.x, transform.position.y+maxRange, transform.position.z);
  30. laser.SetPosition(1, endOfLaser);
  31. lasBegin=transform.position;
  32. lasEnd=endOfLaser;
  33. }
  34. if (normalizeUV==true) //if normalizing the UV. Important when the ray has some pattern, that it doesn't get stretched.
  35. {
  36. var distance = Vector3.Distance(lasBegin, lasEnd);
  37. renderer.materials[0].mainTextureScale.x=distance/normalizeUvLength;
  38. }
  39. }
  40. function Update() //enlarging, alpha stuff
  41. {
  42. time+=Time.deltaTime;
  43. alpha=beginTintAlpha-fadeSpeed*time;
  44. laserSize=(enlargeSpeed*Time.deltaTime)+laserSize;
  45. laser.SetWidth(laserSize,laserSize);
  46. laser.renderer.material.SetColor ("_TintColor", Color(myColor.r, myColor.g, myColor.b ,alpha));
  47. }