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 AKJ9 · Aug 26, 2014 at 09:08 PM · c#collisionrigidbodycolliderbox collider

Getting a sword in place while attacking an enemy

I'm using scripts for my character's sword that I previously used for another project of mine, except it was for a fireball. I want the sword to damage an enemy, and stay in my character's hand. The damage works, although the sword flies off around the scene should it come in contact with the floor or an enemy.

If I turn the Is Kinematic Rigidbody setting on for my sword, the sword stays in place except the enemy does not react to being hit by the sword. So I can achieve one of the two goals depending on the Is Kinematic option. Please review my script and let me know what I am doing wrong for this to happen. The only relevant script that could be part of the problem is this script that's placed on the enemy to detect the sword:

 using UnityEngine;
     using System.Collections;
 
     public class BossScript : MonoBehaviour {
         public static bool dead;
         public AudioClip hitSound;
 
         void Awake()
         {
 
             print ("attached to: "+ name);
         }
 
         public int bossHealth = 3;
         void OnCollisionEnter (Collision collision){
             if(collision.gameObject.tag == "Sword" && CharacterAnimationController.attacking==true){
                 animation.Play("flinch");
                 audio.PlayOneShot(hitSound);
                 bossHealth--; 
                 Debug.Log("reduced health to: " + bossHealth);
                 
                 if(bossHealth<=0){
                     dead = true;
                     animation.Play("dead");
                 } } }

Also bear in mind the following properties:

Sword:

Box Collider (Is Trigger unticked), Rigidbody (Use Gravity and Is Kinematic unticked)

Enemy:

Box Collider (Is Trigger unticked), Rigidbody (Use Gravity and Is Kinematic ticked)

Do I need both a Box Collider and a Rigidbody on both objects? As mentioned, I am using the script I used for my previous project's fireball, and that worked perfectly (although the fireball moved when fired, whereas the sword should stay put in my character's hand).

Many thanks for your time and help in advance.

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
0
Best Answer

Answer by AKJ9 · Aug 26, 2014 at 10:58 PM

Ok I've fixed the issue!

Ticking the X Y X checkboxes for both Freeze Position and Freeze Rotation under the Constraints option fixed this. However, I have another issue. The sword being a solid object distorts my character's movement - he stutters as he runs. The physics of the sword also makes my character bump up as he rolls on the ground.

Now I've noticed that this only happens when the sword's Box Collider is enabled (which it needs to be based on the goals stated above), however I can't see exactly where the problem lies. The sword doesn't touch the floor, and my character doesn't have a Box Collider or Rigidbody himself. He only has a Character Controller - could that be it?

Please help where you can, and thank you once again!

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 JustFun · Aug 26, 2014 at 09:35 PM

Add Fixed Joint to you character and set Connected body setting in joint's properties to your sword. You can set Break force and Break torque to some values, if you want, for example, the sword fly out from the hand, if the strike is too strong.

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 AKJ9 · Aug 27, 2014 at 08:31 AM 0
Share

Thanks @JustFun. I've added a FixedJoint to my character's hand (which automatically adds a Rigidbody to his hand), and dragged my sword into the ConnectedBody field. However, I still get the same problem. Please bear in $$anonymous$$d that the sword is already a child of the hand (parent). Is this a problem?

avatar image AKJ9 · Aug 27, 2014 at 10:28 AM 0
Share

@JustFun I've even tried adding a FixedJoint to my whole character - but still no luck. The sword jerks his movement while running/rolling.

avatar image
0

Answer by AKJ9 · Aug 27, 2014 at 11:57 AM

Done it - assigning each GameObject to a layer and unticking them to cancel their collisions fixed the issue! :-)

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

2 People are following this question.

avatar image avatar image

Related Questions

Why object goes sometimes through walls by adding force ? 2 Answers

How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C# 3 Answers

Stop sword from distorting character animation 3 Answers

Objects with colliders going through walls and each other. 3 Answers

Any way to ignore collision between rigidbodies and colliders/character controllers? 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