Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by yvyv1000 · Oct 21, 2015 at 12:01 PM · collisionrigidbodyscript.ignorecollision

projectile must ignore collision with itself

i have this scirpt

  var projectile : Rigidbody;
  var speed = 100;
  var barrel : Transform;
  var shots :  int  =  0 ;
  var maxShots :  int  =  1 ;
  
  function Update () {
      fwd = transform.TransformDirection(Vector3.forward);
  
  if ( Input.GetButtonDown ("Fire1") && shots < maxShots){
         Shoot();
  }
  else if (shots >= maxShots)
      {
         Reload();
      }
  }
  
  function Shoot(){
          var bullet1 : Rigidbody;
         Physics.IgnoreLayerCollision(8,8, true);
         bullet1 = Instantiate(projectile, barrel.position, barrel.rotation) as Rigidbody;
         bullet1.GetComponent(Rigidbody).useGravity = true;
         bullet1.GetComponent(Rigidbody).AddForce(barrel.forward * speed, ForceMode.Impulse);
  shots ++;
  }
  
  function Reload() {
   yield WaitForSeconds(0.5); 
          shots = 0;
  }

which will instantiate a projectile and fire it while ignoring it' s own collsion this is because my projectile will spawn inside itself in the game if someone is going to say that i could just move the spawn out of the projectile my answer is i can't do that because of a animation i have for throwing the object (this animation is not in the script just yet ill implement it later) so i used a ignorelayercollision to prevent it from colliding with himself but whe i use it, it gives this error and wont spawn the clones

NullReferenceException: Object reference not set to an instance of an object StoneFire.Shoot () (at Assets/MY Assets/Scripts/StoneFire.js:21) StoneFire.Update () (at Assets/MY Assets/Scripts/StoneFire.js:11)

i have this same error on line 18 too but that one does not mess up my in game actions so i wont fix that now (if someone knows how to fix it you can say that too) can someone please tell me why my projectile won't spawn and give that error

projectile information: it is a rigidbody is kanematic is off use gravity is off in editor but script enables it uses sphere collider and not a mesh collider because then i had to put is kanematic on and that would lead to a not moving bullet.

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 yvyv1000 · Oct 21, 2015 at 10:58 AM 0
Share

i also checked the matrix and unchecked the thing correctly

avatar image yvyv1000 · Oct 22, 2015 at 08:15 AM 0
Share

i have made a picture of the scene now all errors are gone but it starts to make clones of clones which is not my purpose cause it's making my pc lagg hard

alt text

the thing that my player is holding is the floating stone with the spawn inside of it it wont shoot the stone tho but the clones

capture3.jpg (177.4 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Oct 21, 2015 at 02:09 PM

There are exactly two possible reasons for why you get a null reference exception inside Shoot:

  • Either you did not assign your projectile prefab to your projectile variable in the inspector

  • or you didn't assign your barrel gameobject to the barrel variable in the inspector

The barrel can also be null when you somehow destroyed the gameobject.

As allen mentioned in his answer it's pointless (though not an error) to use GetComponent(Rigidbody) since your bullet1 variable is already a rigidbody.

A rigidbody actually can't collide with itself. Do you actually use prefab or do you clone an instance that is already in your game?

edit
Reading your comment above it seems that you indeed have an instance of your projectile already inside your scene. Is this some kind of rocket launcher? If so i would suggest:

  • make sure you have your rocket as prefab in your project

  • assign the prefab to the projectile variable

  • delete the projectile from your scene so you only have your weapon there without the projectile

  • add another variable that will hold the "current" loaded projectile

  • when you "reload" your weapon you actually instantiate a projectile at the barrel position and parent it to your weapon. Store the reference to that projectile inside the "current" variable. Now the weapon is loaded.

  • When you fire your weapon you simply check if "current" is not null and if so you simply unparent the "current" object, and launch it by setting useGravity = true and apply your launch force.

  • Once launched you want to set "current" to null.

  • You can't fire again until a new projectil has been instantiated when you call reload

If it's a "normal" weapon you should do:

  • make sure you have your bullet as prefab in your project

  • assign the prefab to the projectile variable

  • delete the projectile from your scene so you only have your weapon there without the projectile

  • when you fire your weapon you simply instantiate the prefab at the barrels position and launch it like you did already.

I guess your bullet has a script attached that destroy things it hits? Make sure you ignore collisions between the barrel and your projectile in case the barrel has a collider. If you don't your projectile might destroy your barrel once you shoot.

Comment
Add comment · Show 3 · 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 yvyv1000 · Oct 21, 2015 at 02:15 PM 0
Share

i clone the instance that is already in my game also i think getcomponent is actually needed cause when i press the Fire1 button my player falls through the terrain i assigned my projectile object (the one that is also in my scene already) and i assigned a barrel to barrel which in this case is my spawn

avatar image yvyv1000 · Oct 21, 2015 at 02:19 PM 0
Share

fixed the falling through terrain

avatar image Bunny83 yvyv1000 · Oct 21, 2015 at 02:30 PM 0
Share

I bet your projectile has hit the terrain? and destroyed the terrain collider? ^^ $$anonymous$$ake sure you setup the collision matix the right way. $$anonymous$$ake sure the projectile can only hit things that it is supposed to hit.

Also inside the projectile script, you should check what it actually hits. You often want that a projectile can hit the terrain, but you don't want the terrain to be destroyed. So make sure you handle the collision the right way.

The easiest way is to use another layer or a tag to "mark" objects that can be destroyed by the projectile so the script on the projectile can check the tag / layer before destroying objects.

avatar image
0

Answer by allenallenallen · Oct 21, 2015 at 01:10 PM

Are you saying that the code works when you take out take out this line?

 Physics.IgnoreLayerCollision(8,8, true);

Because I can see that it still wouldn't work. Your "bullet1" variable is a Rigidbody. Which means you don't need to get the Rigidbody components at all. You're trying to get components that don't exist: A rigidbody within a rigidbody.

 bullet1.useGravity = true;
 bullet1.AddForce(barrel.forward * speed, ForceMode.Impulse);
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 yvyv1000 · Oct 21, 2015 at 02:03 PM 0
Share

no it does not work without that line cause then it will shoot but my spawn (and projectile which is floating there also) will get hit by the just created clone and float away but if i put that line in my code it gives the error and won't instantiate anything

avatar image yvyv1000 · Oct 21, 2015 at 02:12 PM 0
Share

i deleted the getcomponent parts in my script but now my player just falls through the terrain when i press the "Fire1" button

avatar image
0

Answer by yvyv1000 · Oct 22, 2015 at 08:09 AM

fixed it with your help i made a prefab of the scene object and assigned that to my projectile var insteed of the scene object i added what mass and now its working like a charm i thank you for everyone who wants to use my script here is the working one:

   var projectile : Rigidbody;
   var speed = 100;
   var barrel : Transform;
   var shots :  int  =  0 ;
   var maxShots :  int  =  1 ;
   
   function Update () {
       fwd = transform.TransformDirection(Vector3.forward);
   
   if ( Input.GetButtonDown ("Fire1") && shots < maxShots){
          Shoot();
   }
   else if (shots >= maxShots)
       {
          Reload();
       }
   }
   
   function Shoot(){
           var bullet1 : Rigidbody;
          Physics.IgnoreLayerCollision(8,8, true);
          bullet1 = Instantiate(projectile, barrel.position, barrel.rotation) as Rigidbody;
          bullet1.GetComponent(Rigidbody).useGravity = true;
          bullet1.GetComponent(Rigidbody).AddForce(barrel.forward * speed, ForceMode.Impulse);
   shots ++;
   }
   
   function Reload() {
    yield WaitForSeconds(0.5); 
           shots = 0;
   }

feel free to edit it or use it 2 things

make sure your projectile is a prefab and if you dont need the layer part delete it to make the script less heavy

peace out :)

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody Collision Question 1 Answer

Player colliding with pickable object 0 Answers

Ignore collision between character controller and a rigid body object 1 Answer

Collision that doesn't apply force between certain rigidbodies 0 Answers

Animator overriding collisions? 0 Answers


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