Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 09, 2013 at 07:44 PM by iamthecoolguy11 for the following reason:

its old

avatar image
0
Question by iamthecoolguy11 · Oct 12, 2013 at 11:31 PM · rigidbodyraycastaddforcegun

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);
     }
     
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TrickyHandz · Oct 13, 2013 at 12:04 AM 0
Share

Can you please post the code that gives you the error and what the error code is.

avatar image iamthecoolguy11 · Oct 13, 2013 at 12:33 AM 0
Share

ok i well post the whole script

1 Reply

  • Sort: 
avatar image
-1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Oct 13, 2013 at 01:51 AM 0
Share

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

avatar image knuckles209cp · Dec 14, 2013 at 06:39 AM 0
Share

yea ok thanks man :)

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a ball stay in the air longer when force is added? 1 Answer

How to aim a ball with cross hair? 2 Answers

Seeking help or Tutorials for Raycast/movement script. 1 Answer

fire to camera 1 Answer

How to fire a gun using raycasting and still add force 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges