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
1
Question by EvanCheddar · Jan 15, 2018 at 11:40 PM · shootingprojectile

,How do I make projectile weapons stick to enemies or targets?

Hi there. Im new to Unity and I could use some help. I would like my projectile to hit and stick to its target. The target is moving, and I would like the projectile to stick into. Sort of like an arrow sticking into a target or a shuriken sticking into a target.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by calpolican · Jan 16, 2018 at 12:16 AM

Well, the best way to do this is probably parenting the object. imagine you have the following elements:

 Transform myProjectile; //your sticky projectile
 Transform target; //the character or thing to wich the projectile is gonna stick.     

once you know that the projectile collided with something you can do:

 miProjectileObject.transform.parent = targetedObject;

Now the projectile will follow the position and rotation of the parent object!

You probably want to do it whenever the projectile touches your object:

     Collider yourCollider;
     
     OnCollisionEnter(Collision collision) //whenver this object collides
     {
     this.parent = collision.transform; // make the object collision object it's parent
     YourCollider.enabled = false; //disable your collider, otherwise it may stick to something else if it touches it.
 //depending on how you're animating it you may one to disable the rigidbody too to freeze it in the position.
     }
 
 
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 EvanCheddar · Jan 20, 2018 at 03:17 PM 0
Share

I've tried this but perhaps I'm still uncertain which order to write the code in. Could you walk me through the code step by step? Thank you so much and I'm sorry if I'm being dumb.

avatar image calpolican EvanCheddar · Jan 25, 2018 at 06:36 AM 0
Share

Oh, no worries. It would be useful to know how you're trhowing your projectile. You have a guy with a collider, right? and you have a projectile with a collider, ok? You want to know when both colliders are colliding. To know this you use the function "OnCollisionEnter()", that function will run whenever a collision takes place. You also pass a parameter (Collision collision), the function will store there the collision information. You'll be able to know what object collided and several other things.

So you put in your projectile a script that has the function:

 OnCollisionEnter(Collision collision){}

Now once the knife hits another collider, either it's a wall, a player, or a moving car, you want it to stick to it. To do this you have to do two things inside the OnColllisionEnter function: 1)freeze the projectile (stop any animations), 2)and parent the projectile to the colliding object. +You have to freeze it (1) because now it's stuck in the surface. You want it to stick in the position it had when it hit the surface. +You have to parent it (2) because you want it to inherit the movement form the object it's stuck into. Parenting is just like puting a file in a Windows folder: if you move the folder, the file with move along. The knife will inherit any of your character's movements, it will be stuck to it.

Now, to freeze the projectile I'd have to know what method you're using to animate it. Say that you're using a rigidbody. You'd then use something like:

 Rigidbody myProjectileRigidbody = this.GetComponent<RigidBody>(); //to store the rigidbody of you projectile inside a variable
 myProjectileRigidbody.velocity = new Vector3.zero; //so that the rigidbody has no velocity (stops)
 myProjectileRigidbody.is$$anonymous$$inematic = true //that basically disables the Rigidbody alltogether.

Now, to parent the projectile with your object:

 this.transform.parent = colision.transform; //looks for your collider object and sets it as the knife parent. So basically now the projectile will be moved by the object (stucked).

Here you close the OnCollisionEnter() function with the closing bracket.

A NOTE: $$anonymous$$eep in $$anonymous$$d that to the world of the game your object is not your model but your collider. The aftermentioned method should work well, BUT, the knife will stuck to your collider, not to your model. The object could never stick to the game object. you can try to stick it to a bone or other hacks, but that's way more advance. If you throw it to a car it will work very well since the collider can be similar to the car shape, but if you throw it to a character and the collider is a capsule collider, the knife will stick to the capsule collider, while the character may have some idle, walking or running animation. To overcome this you can place your collider pretty tight following your model torso and head. Still it won't be perefect. To make it perfect it will require a whole bunch of extra expanations, but I hope this sends you in the right direction.

avatar image
0

Answer by RedDeCipher · Jan 16, 2018 at 12:03 AM

   private Vector3 refPos;
   private GameObject target;
   private bool sticked = false;

   void OnCollisionEnter(Collision collision)
         {
             ContactPoint contact = collision.contacts[0];
            target = collision.gameObject;
             Vector3 refPos = contact.point - target.transform.position;
             sticked = true;
             //.....Other stuff you wana do when collided
           
         }
 
     void Update(){
 
     //... Other Update Stuff
 
     if(sticked)
          transform.position = target.transform.position + refPos;
 
 
 }





Comment
Add comment · Show 1 · 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 EvanCheddar · Jan 16, 2018 at 04:10 AM 0
Share

This works although the arrow doesn't stick to where it hits, it automatically places the arrow in the middle of the target, it also increases the speed once the arrow hits the moving target and carries it away twice as fast.

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

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

Related Questions

Shooting in 2 dimensional games 2 Answers

Gun Fire, sparks on Collision 1 Answer

Shooting projectiles once per second. 1 Answer

Help with getting a bullet to move. 2 Answers

How do I get my character to shoot a projectile in the direction of the cursor? 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