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 zmar0519 · Apr 19, 2011 at 01:00 AM · physicsweaponsword

Sword logic script not working

Hi, everyone. I have a sword logic script running on my players sword, and it should be adding force. There are no components out of the ordinary in my game save two scripts: my inventory and my item script. Both can be viewed here. The problem is sometimes it works fine, sometimes there is a delay and then it adds force, and sometimes it doesn't respond. All help is appreciated. Here is the sword logic script:

var swordPoints : Transform[]; var swingPower : int; var showGizmosInEditor : boolean = false;

function Update () { var hit : RaycastHit; for(i = 0; i < swordPoints.length; i++) { if(Physics.Linecast(transform.position, swordPoints[i].position, hit)) { Hit(swordPoints[i], hit); } } } function Hit(i : Transform, hit : RaycastHit) { if(hit.collider.rigidbody) { hit.collider.rigidbody.AddExplosionForce(swingPower, i.transform.position, 1.0, 3.0); } if(hit.collider.tag == "Enemy") { hit.collider.gameObject.SendMessageUpwards("ApplyDamage", swingPower, SendMessageOptions.DontRequireReceiver); } } function OnDrawGizmosSelected() { Gizmos.color = Color.blue; if(showGizmosInEditor) { if(swordPoints.length != 0) { for(i = 0; i < swordPoints.length; i++) { if(swordPoints[i] != null) { Gizmos.DrawLine(transform.position, swordPoints[i].position); } } } } }

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 Bravini · May 02, 2011 at 06:34 PM

I'd consider instead of using AddExplosion to just activate hit animations on the enemies. AddExplosion should be used on a coroutine and expand from the sword to the outside, not called when objects are hit by linecast. You're making things explode from inside themselves.

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
1

Answer by Ashkan_gc · May 02, 2011 at 02:23 AM

your script is completely correct and has this logic in it.

there are a set of transforms (points) which you cast a line from the pivot of your sword toward them and then if hit anything, you check if the hitpoint has a rigidbody you apply force to it. then if it also has the enemy you also send a message to it.

there are two things to consider.

  1. objects should not be in "ignore raycast" layer for raycasting to work with them.
  2. the transform points must be some points that move with your sword when you move the character (i.e children of your sword or object), otherwise the raycasting will be done toward incorrect points.

the second problem is easily visually debugable because of your gizmo part so you don't have this problem so check if your layers of objects is correct and if they have a rigidbody and their irigidbody is not kinematic. if it's correct then play with the force value, maybe it's too low and check if your objects have ApplyDamage and if yes then check if it's called or not.

place print statements inside your ifs to see if they are called or not (above addforce and sendmessage).

if not solved yet, then comment here and i'll provide more help!

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
-1

Answer by GesterX · May 02, 2011 at 12:41 AM

I may be wrong but I think it's because you're calling Hit inside Update(). As far as I am aware all physics based stuff needs to be called in FixedUpdate().

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 Ashkan_gc · May 02, 2011 at 02:15 AM 0
Share

raycasting can be done safely in Update and calling the AddForce method group in Update is possible however not recommended.

avatar image GesterX · May 02, 2011 at 02:20 AM 0
Share

I stand corrected then. I was just referring to AddForce.

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

No one has followed this question yet.

Related Questions

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

how do i make a weapon swing like a crowbar or a baseball bat 2 Answers

If statment to check if player is within collider 1 Answer

Parenting player to car on click. 1 Answer

Raycast length must be equal to the target distance 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