FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.

Share

descriptionRaycast Shooting Script

more_horiz
So I downloaded a raycast shooting script from some place on the internet and I waned to know if there was a way to make the max distance of each shot changeable. Basically, I want to have control over how far the shots go.
The following is the program:

Code:

#pragma strict

var Effect : Transform;
var TheDammage = 100;

function Update () {
   
   var hit : RaycastHit;
   var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
   
   if (Input.GetMouseButtonDown(0))
   {
      if (Physics.Raycast (ray, hit, 100))
      {
         var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
         Destroy(particleClone.gameObject, 2);
         hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
      }
   }
   
}



Thanks!

_________________
-One Knife Games

descriptionRe: Raycast Shooting Script

more_horiz
I really recommend you read the raycast reference, not only will it help you understand how to limit vector pos/ distance but also you would understand the concept of raycast furthermore.. ray length is covered in this documentation.

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

descriptionRe: Raycast Shooting Script

more_horiz

Code:


function Update() {
   var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
   var hit : RaycastHit;
   if (Physics.Raycast (ray, hit, 100)) {
      Debug.DrawLine (ray.origin, hit.point);
   }
}



this was pulled directly from the API it self, (ray,hit,100) the 100 is the distance of the raycast. yours is the same, change that to a float value e.g

Code:



var dist = 50;
if (Physics.Raycast (ray, hit, dist)) {




_________________
https://www.youtube.com/channel/UCrS-nC8CYukMmhAKpyq6TCA
Permissions in this forum:
You cannot reply to topics in this forum