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 /
avatar image
0
Question by KnC_Studios · Jul 29, 2013 at 07:37 PM · fpsgrenadesticky

How do you make a grenade stick to different surfaces?

Hello! I have fps and I am wanting to make grenades. I have them throwing right but I want them to stick to enemies but just bounce off walls and boxes and such. I have a script but it just sticks to rigidbodies. I want it to stick only to enemies, and currently they are not rigidbodies. I am not the best coder in the world so i'd like some help.

Thanks in advance!

 /// This script requires that a rigdibody is attached to the same game object
 
 private var localAttachmentPoint = Vector3.zero;
 private var attachedTransform : Rigidbody;
 private var isAttached = false;
 
 // Enable only when we have a collision
 function Awake ()
 {
     enabled = false;
 }
 
 function OnCollisionEnter ( col : Collision)
 {
     if (isAttached)
         return;
     isAttached = true;
 
     // When we hit a rigidbody we attach to it with a fixed joint
     // this gives extra realism eg. the grenade's mass will now pull down the attached to rigidbody
     if (col.rigidbody)
     {
         var joint = gameObject.AddComponent("FixedJoint");
         joint.connectedBody = col.rigidbody;
     }
     // When we hit a normal collider we just follow the transform around!
     
     else
     {
         // Store local attachment point and transform we stick to    
         attachedTransform = col.rigidbody;
         localAttachmentPoint = attachedTransform.InverseTransformPoint(rigidbody.position);
     
         // The grenade's position is now driven by the script instead of physics
         rigidbody.isKinematic = true;
         enabled = true;
     }
 }
 
 function Update ()
 {
     // Update position to follow the object we stick to
     if (attachedTransform)
     {
         transform.position = attachedTransform.TransformPoint(localAttachmentPoint);
     }
     else
     {
         // The attached transform was destroyed. Let go and enable physics control
         rigidbody.isKinematic = false;
         enabled = false;
     }
 }
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

1 Reply

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

Answer by perchik · Jul 29, 2013 at 07:44 PM

Add a tag [in the editor] to objects that you want to stick to. Then on collision, instead of checking col.rigidbody compare against col.gameObject.tag

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 KnC_Studios · Jul 29, 2013 at 07:47 PM 0
Share

Could you please explain? I do not know how to add a tag. I'm new to scripting.

avatar image perchik · Jul 29, 2013 at 07:58 PM 0
Share

No worries, it's a unity thing. This article explains it better than I can. Basically, you can set a tag for a gameobject that labels it as something. In your case, you could tag sticky objects as "Sticky". Then on collision, you can say if(col.gameObject.tag == "Sticky") { //do something} else {bounce off}

avatar image KnC_Studios · Jul 29, 2013 at 10:16 PM 0
Share

Thank you!

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

ufps package freezes when adding inventory system 0 Answers

When my button is pressed the action reapeats 1 Answer

Fade to white after being killed - iPhone game 2 Answers

need a spawn script 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