Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by gravelpootis · Sep 18, 2021 at 09:43 AM · physicscollision detectionkinematicbullets

Speculative CCD without rigidbody

I am making bullet physics through script without rigidbody using raycast and I wanna know how to implement Speculative CCD for it because bullets keep passing through at high speeds.

this is my bullet script

 public class C_BaseKinematicEntity : MonoBehaviour
 {
     public C_BaseKinematicEntity(GameObject entity, EntityProperties_t entityProperties)
     {
         this.entity = entity;
         this.mass = entityProperties.mass;
         this.drag = entityProperties.drag;
         this.angularDrag = entityProperties.angularDrag;
     }
 
     public GameObject entity;
     public float drag, angularDrag, mass;
     public Vector3 position, velocity, angularVelocity;
     public Quaternion rotation;
 
     public void Setup(EntityData_t entityData)
     {
         this.position = entityData.position;
         this.rotation = entityData.rotation;
         this.velocity = entityData.velocity;
         this.angularVelocity = entityData.angularVelocity;
 
         this.UpdateGameObject();
     }
 
     public void UpdateGameObject()
     {
         this.entity.transform.position = this.position;
         this.entity.transform.rotation = this.rotation;
     }
     public void UpdateVelocity()
     {
         this.velocity += Physics.gravity * Time.fixedDeltaTime;
         this.velocity *= (1 - Time.fixedDeltaTime * this.drag);
 
         Util.Render.DebugCube(this.position + this.velocity, 0.1f, Color.grey);
     }
     public void UpdatePosition()
     {
         this.position += this.velocity * Time.fixedDeltaTime;
     }
     public void UpdateRotation()
     {
         this.rotation = Quaternion.FromToRotation(Vector3.forward, this.velocity);
     }
     public void UpdateCollision()
     {
         float maxDistance = this.velocity.magnitude * Time.fixedDeltaTime;
 
         RaycastHit hit;
 
         if (Physics.BoxCast(this.entity.transform.position, this.entity.transform.lossyScale / 2, this.entity.transform.forward, out hit, this.entity.transform.rotation, maxDistance))
         {
             ProcessCollision(hit);
         }
     }
     public virtual void ProcessCollision(RaycastHit hit) { }
 }
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
0

Answer by Edy · Sep 18, 2021 at 01:02 PM

You can take the current bullet position, the previous bullet position, and call Physics.RaycastAll() or Physics.SphereCastAll() between both positions. That would give you all objects the bullet has passed through in that timestep.

Comment
Add comment · Show 3 · 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 gravelpootis · Sep 18, 2021 at 01:15 PM 0
Share

Sorry, that doesn't sound like right solution. I need something like interpolation, I need to smooth out the calls of bullet physics at a fixed frame rate.

avatar image Edy gravelpootis · Sep 18, 2021 at 08:07 PM 0
Share

Interpolation is a different thing. It won't give you CCD. You have to deal with discrete time steps of arbitrary duration, and interpolation doesn't guarantee your bullet won't keep passing through objects.

Casting the objects between previous and current position guarantees to detect all the affected objects, at any speed, under any time step. Then you could sort the hits by distance to the initial position and use the first hit only, or iterate over the hits to create the effect of the bullet trespassing objects (i.e. breaking a window, then hitting something behind it, everything in a single fixed step.

avatar image gravelpootis Edy · Sep 19, 2021 at 08:06 AM 0
Share

Okay, but I can't imagine the code, can you show me an example please and I dont plan to use RaycastAll because purpose of doing bullets like this with object pooling is high performance, but doing raycastAll on every single bullet will cause performance drop

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

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

Freezing an object and keeping it's velocity 1 Answer

Object refuses to collide 0 Answers

What is the point of MovePosition()? 1 Answer

Rigidbody is colliding terrain on isKinematic = false. call although meshes don't touch 0 Answers

How do I keep rigidbodies from pushing each other? 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