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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Raycast Shooting Script

Raycast Shooting Script

View previous topic View next topic Go down  Message [Page 1 of 1]

1 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:
#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);


View user profile http://oneknifegames.weebly.com/

2 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.


View user profile http://www.youtube.com/user/misterninjaboy

3 Re: Raycast Shooting Script on Fri Sep 11, 2015 7:40 pm


Member [Lv. 3]
Member [Lv. 3]

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


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

View user profile http://jakeaquilina.weebly.com/

Sponsored content

View previous topic View next topic Back to top  Message [Page 1 of 1]

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