UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionRaycast Shooting Script EmptyRaycast 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!

descriptionRaycast Shooting Script EmptyRe: 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

descriptionRaycast Shooting Script EmptyRe: 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)) {



descriptionRaycast Shooting Script EmptyRe: Raycast Shooting Script

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply