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 JustinReinhart · May 25, 2013 at 02:33 AM · collisionphysicscollisionhandling

How do I stop a projectile cold when colliding with another object? (projectile shifts on contact)

This is an attempt to do a simple arrow-stuck-in-wall simulation. My question pertains to how I can get a more accurate reaction out of the collision.

I'm Instantiating an Arrow prefab in front of a Third Person Controller, and only in the Start function of the arrow I am applying rigidbody force in the forward direction. I am having no problem detecting the collison, or bringing the object to a halt, but I am having a problem with the projectile shifting after the collision. This seems to occur before I have a chance to freeze the object.

This is particularly noticeable with an elongated projectile (such as a stretched capsule or cube) shot almost parallel to the target wall; In this scenario, the projectile will sometimes rotate 90 degrees as if it were bouncing off the surface before being frozen. Sometimes it might pick a nearby location to teleport to. Ideally, if the "arrow" were frozen exactly where it collided, then it should still be facing the camera perfectly straight. Dig?

:) Please, any tips you can offer for perfecting the behavior and getting my arrows to stay still would be helpful. I feel I may be missing something fundamental about Unity.

 public class Arrow : MonoBehaviour {
     
     Transform MyTransform;
     Rigidbody MyRigidbody;
     
     // Use this for initialization
     void Start () {            
         MyTransform = this.transform;
         MyRigidbody = this.rigidbody;
         MyRigidbody.AddForce(MyTransform.forward * 50, ForceMode.VelocityChange);            
     }
 
     void OnCollisionEnter()
     {        
         MyRigidbody.isKinematic = true;                    
     }
 }

Unity 3.5

[1]: /storage/temp/11363-tryarrowsunity.zip

tryarrowsunity.zip (33.6 kB)
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 robertbu · May 25, 2013 at 04:38 AM

This issue has come across this list a couple of times in the last month, and I don't remember any reasonable solutions. So since you were thoughtful enough to provide a project, I thought I'd take a run at a solution. I came up with two workarounds. To me they are a bit hackish, but they appear to work. For both solutions start by doing:

  • From the edit menu, select Project Settings/Time. Reduce the Fixed Timestep from 0.02 to 0.01.

  • Select the Arrow prefab. Change the Collision Detection to Continuous Dynamic.

The first solution involves saving the position and rotation and restoring it if there is a hit. Here is the modified version of your script:

 using UnityEngine;
 using System.Collections;
 
 public class Arrow : MonoBehaviour {
     private Quaternion q;
     private Vector3 v3;
     private bool hasHit = false;
     Transform MyTransform;
     Rigidbody MyRigidbody;
     
     // Use this for initialization
     void Start () {            
         MyTransform = this.transform;
         MyRigidbody = this.rigidbody;
         MyRigidbody.AddForce(MyTransform.forward * 50, ForceMode.VelocityChange);            
     }
 
     void OnCollisionEnter(Collision col)
     {        
         MyRigidbody.isKinematic = true;    
         hasHit = true;
     }
     
     void LateUpdate() {
         if (hasHit) {
             transform.position = v3;
             transform.rotation = q;
             hasHit = false;
         }
         else {
             v3 = transform.position;
             q = transform.rotation;
         }
     }
 }


For the second solution:

  • Enable the the 'IsTrigger' setting for the colliders for all four walls.

  • Use your original script, but change the OnCollisionEnter() to OnTriggerEnter().

Note for both of these solutions, you can adjust the center of the collider. For the first solution, push the collider back inside the wall. The result will be that the arrows will stick in the walls rather than be on the surface. For the OnTriggerEnter() solution, I pulled the colliders a bit forward into the room.

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 JustinReinhart · May 30, 2013 at 08:29 PM 0
Share

Thank you, Robert. Your answer went "above and beyond." Using LateUpdate to capture/restore the object's previous position is very clever and seems to work perfectly.

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

13 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

Related Questions

Collision Handling (Pushout and Sliding) for Customized Collision Object? 0 Answers

AddForce to a point of a GameObject, not the center 1 Answer

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

Punching a crate 0 Answers

Imported 3D models collisions 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