- Home /
its old
Add force to a rigid body with raycast
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);
}
}
Can you please post the code that gives you the error and what the error code is.
Answer by knuckles209cp · Oct 13, 2013 at 12:51 AM
heres a Rigidbody detection ( add force script ) out of my head
function RayShoot (){
//If you have a shoot animation to play when you want the detection to happen
GameObject.Find("Your gun or whatevers parent").animation.Play("shoot animation");
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * Range , Color.red);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
if(Hit.rigidbody){
if( PHitParticle) {
PHitParticle.transform.position = Hit.point;
PHitParticle.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
PHitParticle.Emit();
Hit.rigidbody.AddForceAtPosition ( DirectionRay * Force , Hit.point);
Hit.collider.SendMessageUpwards( "ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
BulletsLeft --;
if(BulletsLeft < 0){
BulletsLeft = 0;
}
if(BulletsLeft == 0){
Reload();
}
}
make an empty game object and calling it RayShoot and apply the script , position the empty game object at the end of the gun and it should work
PS : If you want to use this code and make a game update or something on youtube give credit to knuckles209cp . Thanks
thanks bro but I got a error when I put it in my script
NullReferenceException: Object reference not set to an instance of an object Gun.Update () (at Assets/_Scripts/Gun.js:31)
I updated my script up top. and lol i can give ya a shout out in the update if you type it in your reply