I made a gun script and it works good and stuff but I can't get it to add force to a rigid body with out getting a error.
heres the main part of the script
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))
{
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
whole script
var clipsLeft = 3;
var AmoLeftInClip = AmoInClips;
var TheDammage = 100;
var AmoInClips = 30;
var shoot : boolean = true;
var Reload : AudioClip;
var Fire : AudioClip;
var Force = 10;
var Range = 900;
function Update () {
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * Range , Color.red);
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if(Input.GetKey(KeyCode.R) && shoot == true)
{
AReload();
}
if (Input.GetMouseButtonDown(0) && shoot == true && AmoLeftInClip > 0)
{
AmoLeftInClip -= 1;
audio.PlayOneShot(Fire);
if (Physics.Raycast (ray, hit, 100))
{
Hit.rigidbody.AddForceAtPosition ( DirectionRay * Force , Hit.point);
// hit.rigidbody.AddForceAtPosition(10 * forward, hit.point);
hit.transform.SendMessage("Hurt", SendMessageOptions.DontRequireReceiver);
}
}
}
function OnGUI()
{
GUI.Label (Rect (0,800,80,20), AmoLeftInClip.ToString(), "box");
GUI.Label (Rect (0,840,80,20), clipsLeft.ToString(), "box");
}
function AReload()
{
if(AmoLeftInClip < AmoInClips && clipsLeft > 0)
{
shoot = false;
audio.PlayOneShot(Reload);
yield WaitForSeconds(1);
clipsLeft -= 1;
AmoLeftInClip = AmoInClips;
shoot = true;
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Amo")
{
clipsLeft += 1;
Destroy(other.gameObject);
}
}
↧