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 DoubleLee · Mar 08, 2015 at 06:29 PM · collisionrigidbodyprojectilemelee

Collision detection for Arrows and Melee. How to properly setup physics/collision?

I'm building a game that involves shooting arrows (which stick to the object hit) and swinging melee weapons like swords/axes.

I've tried many different combinations of colliders trigger/non, rigidbodies kinematic/non. I've tried adding a rigid body for every collider, I've tried adding just 1 parent rigid body for each NPC with compound colliders on the mesh children beneath which are animated.

Each way I have tried this had some draw back or didn't work quite right. For example, when using just triggers my arrows will stop not where they originally should of hit, but some where farther probably where the arrow was when the frame started that detected the trigger.

When using colliders (non trigger) I found that only non-kinematic rigidbodies will fire collisions, so then I get very strange behavior with my swords swing animations mixing with physics ( they fly around ).

So what is the proper setup for physics of arrows and melee weapons such as swords?

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
1

Answer by Cherno · Mar 08, 2015 at 09:55 PM

For accurate collision detection with fast-moving objects like your arrows, you need to customize a bit:

 public LayerMask layerMask;
 private bool impacted = false;
 private float skinWidth  = 0.1f; //probably doesn't need to be changed 
     private float minimumExtent; 
     private float partialExtent; 
     private float sqrMinimumExtent; 
     private Vector3 previousPosition; 
     private Rigidbody myRigidbody;
 
     private Vector3 lastPosition;
 
 void Awake() { 
         myRigidbody = GetComponent<Rigidbody>();
         previousPosition = myRigidbody.position; 
         minimumExtent = Mathf.Min(Mathf.Min(GetComponent<Collider>().bounds.extents.x, GetComponent<Collider>().bounds.extents.y), GetComponent<Collider>().bounds.extents.z); 
         partialExtent = minimumExtent * (1.0f - skinWidth); 
         sqrMinimumExtent = minimumExtent * minimumExtent; 
 
     }
 
 
 void FixedUpdate() {
         
 
             Vector3 direction = transform.position - lastPosition;
             Ray ray = new Ray(lastPosition, direction);
             RaycastHit hit = new RaycastHit();
             if (Physics.Raycast(ray, out hit, direction.magnitude, layerMask))    {
                 Impact(hit.point, hit.normal, hit.collider.gameObject);
             }
             
             this.lastPosition = transform.position;
         }
     }
 
 void OnCollisionEnter(Collision collision) {
         
         if(impacted == true) {
             return;
         }
         Impact(collision.contacts[0].point, collision.contacts[0].normal, collision.collider.gameObject);
         impacted = true;
     }

 void Impact(Vector3 pos, Vector3 normal, GameObject hitObject) {
                 //Apply damage to the hit gameObject, or whatever
         Destroy(gameObject);
     }
     


Kinematic rigidbodies indeed don't reigster collisions. If you want to make a rigidbody register a collision with another collider without making it react to physics, set the other collider to be IsTrigger = true. You can also use things like SphereCastAll to hit multiple enemies in front of you when you swing your sword, or just check if there's actually one.

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 HappySlice · Sep 05, 2015 at 05:32 PM 0
Share

What is this Impact() function you're calling? LOL nvm

avatar image
1

Answer by JannickL · Feb 22, 2018 at 05:37 PM

Hey there, this works flawless for me (pretty simple but effective):

Actually i'm sending a ray from the arrows position to its forward vector to check if he hits something. However you should only use that code for a skyrim like archery system (sending the ray costs some performance).

 using UnityEngine;
 
 public class Arrow : MonoBehaviour {
 
     public Transform tipTransform;

     public GameObject _explosionEffect;
 
     private AudioSource _audioSource;
     private Rigidbody _rb;    
 
     private void Awake() {
         _rb = GetComponent<Rigidbody>();
         _audioSource = GetComponent<AudioSource>();

         // Set the center of mass to the arrows tip to make the arrow flight realistic
         _rb.centerOfMass = tipTransform.localPosition;
 
         Destroy(gameObject, 5f); // Make sure arrow gets always destroyed
     }
 
     private void FixedUpdate() {
         RaycastHit hit;

         // Send raycast from arrows position to is forward vector to detect if something gets hit
         if(Physics.Raycast(_rb.position, _rb.transform.forward, out hit, 2f))
             Explosion(hit.collider, hit.point);
     }
 
     private void Explosion(Collider other, Vector3 point) {
         Debug.Log("Arrow hitted: " + other.transform.name);
 
         // Spawn an explosion effect
         Destroy(Instantiate(_explosionEffect, point, Quaternion.identity), 2f);
 
         Destroy(gameObject);
     }
 }

Greets

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 ifisch · Mar 08, 2015 at 08:07 PM

This might help http://docs.unity3d.com/Manual/CollidersOverview.html

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

24 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

Related Questions

Innacurate terrain collision with fast rigidbody 1 Answer

Dart projectile collide with another dart in the board 0 Answers

Bullet projectiles with collision info without affecting others 0 Answers

Returning Force/Direction to zero 0 Answers

Colliding Melee Weapons 2 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