• 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
2
Question by insomain · Nov 27, 2011 at 03:08 AM · collisionphysicstriggerguncharacter controller

Gun collision problem

I am trying to create a gun prefab.

I want the gun to simulate physics but not affect my character controller. I also want the player to be prompted to pick up the gun when the character controller collides with it.

I have tried putting the player and guns on different layers, letting them both collide with the default layer but not with each other. The problem is that this disables collision detection all together and makes it so I can't detect if the player can pick up the gun.

Any help appreciated as to how I should approach this, I am very new to unity. Thanks!

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

3 Replies

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

Answer by syclamoth · Nov 27, 2011 at 03:30 AM

You're on the right track, here. You should create a seperate trigger volume that 'floats' above the physics-controlled gun prefab, and manages the 'pick-up' behaviour, but doesn't affect physics. Don't make it a transform child of the gun, exactly, but make it stick to the gun's position using

 transform.position = gun.transform.position

every frame (in FixedUpdate). Then, when the player touches it and the correct conditions are met, do something like this

 player.GiveGun(gun);
 Destroy(gun.gameObject);
 Destroy(gameObject);

to clean up the trigger and the associated rigidbody.

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
avatar image
0

Answer by insomain · Nov 27, 2011 at 03:58 AM

Thank you guys very much, it works perfectly!

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
avatar image
2

Answer by aldonaletto · Nov 27, 2011 at 03:33 AM

Usually you should use a trigger to pick up things: create a cube, adjust its dimensions to the volume you want, set Is Trigger in the Collider component, child the weapon to it, then disable the Mesh Renderer component to make the trigger invisible. When the player touches the trigger volume, a OnTriggerEnter event is called in both, the player and the trigger scripts. You can destroy the picked object using the trigger script, and enable the picked weapon in the player script:

// in the trigger script: function OnTriggerEnter(col: Collider){ if (col.tag == "Player"){ Destroy(gameObject); // destroy the picked item } }

// in the player script: function OnTriggerEnter(col: Collider){ if (col.name == "PickMachineGun"){ // enable weapon "machine gun" } else if (col.name == "PickRocketLauncher"){ // enable weapon "rocket launcher" } ... }

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 syclamoth · Nov 27, 2011 at 03:43 AM 0
Share

Problem here is that @insomain needs the gun to have physical behaviour, as well. But I guess my answer covers that part of it.

avatar image aldonaletto · Nov 27, 2011 at 03:55 AM 0
Share

I just didn't understand what he intended with this physics thing, thus I ignored it and showed the conventional stuff - but your answer is a good solution for a rigidbody weapon: the weapon can fall to the ground, bounce etc. and still be picked by the player.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

colliders with trigger on not working? 1 Answer

How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers

Manual response when 2 shapes made of rectangular volumes collide. 0 Answers

Trigger Colliders: adding/removing rigidbody/collider at runtime is unreliable or confusing 1 Answer

Wind physics script 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