Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1858
Rep : 3
Join date : 2013-12-21
Age : 15
Location : USA
View user profilehttp://oneknifegames.weebly.com/

Raycast Shooting Script

on Mon May 11, 2015 4:05 pm
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!
avatar
Admin
Admin
Posts : 608
Points : 3170
Rep : 406
Join date : 2013-12-11
Age : 21
View user profilehttps://www.youtube.com/user/misterninjaboy

Re: Raycast Shooting Script

on Tue May 12, 2015 10:35 pm
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
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 45
Points : 1398
Rep : 3
Join date : 2015-08-30
Age : 23
Location : Melbourne
View user profilehttp://jakeaquilina.weebly.com/

Re: Raycast Shooting Script

on Fri Sep 11, 2015 7:40 pm
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)) {


Back to top
Permissions in this forum:
You cannot reply to topics in this forum