• 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
2
Question by GoMeteoroGo · Oct 03, 2013 at 05:38 PM · raycastcollidercollison

Colliders overlapping

I feel like t$$anonymous$$s question MUST have been answered somewhere before but after a few hours of research I can't find anyt$$anonymous$$ng so, here I am!

Just starting out, I am just doing some experimenting on how to get collision detection in a game to work properly. I have two objects in my world, both with box colliders, arranged in a vertical line. The upper collider is a Rigidbody and at startup, I assign it a velocity straight down with a speed of 1. It falls until it $$anonymous$$ts the lower object and then stops, but it doesn't stop early enough. For one frame, it partially overlaps the lower object, before "bouncing" back to the position it should occupy in the next frame. about 20% of the objects overlap.

In trying to solve that issue so that the object stops when its supposed to, I've given it the following code:

 using UnityEngine;
 using System.Collections;
 
 public class down : MonoBehaviour {
     
     private float length = (1f);
     
     void Start () {
     rigidbody.velocity = Vector3.down;
     }
     
     void fixedUpdate () {
     RaycastHit $$anonymous$$t;
     Ray landingRay = new Ray(transform.position, Vector3.down);
         //cast a ray from the object's origin downward with a length of one unit
         
             if (Physics.Raycast(landingRay, out $$anonymous$$t, length))
             {
                 if ($$anonymous$$t.distance < 0.5f)
                 transform.position = transform.position + (Vector3.up*(0.5f - $$anonymous$$t.distance));
             }
             }
     //When the ray $$anonymous$$ts an object, check and see if the length between the ray's origin and
     //the $$anonymous$$t object is less than 0.5, or one half of the falling object's diameter. 
     //If it is, move the object upward by the difference.
 }

The idea being, if the distance from the falling object's origin is less than half of its diameter, then it must be overlapping the other object, so it needs to move up until that's no longer true.

T$$anonymous$$s logic seems sound to me but t$$anonymous$$s code does not appear to have any meaningful effect on the movement of my object. So:

-For my own understanding, what's the flaw in my reasoning?

-More practically, how can I make the object stop when it $$anonymous$$ts another collider, instead of going into the collider, then popping into place on the next frame?

Comment
Add comment · Show 1
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 Marsupilami · Oct 03, 2013 at 06:57 PM 0
Share

You could try reducing the velocity if the object is within range of hitting another object in the next frame.

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by robertbu · Oct 03, 2013 at 07:10 PM

First off, your raycasting is not working because "fixedUpdate()" should be "FixedUpdate()" with an upper case 'F'.

Other than raycasting, the other methods mentioned on t$$anonymous$$s list to address t$$anonymous$$s problem are:

1) Setting Min Penalty for Penetration to 0.0: Edit>Project Settings>Physics

2) Reducing the Time.fixedTimeStep: Edit>Project Settings>Time. Try reducing it from 0.02 to 0.01.

Comment
Add comment · Show 4 · 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 GoMeteoroGo · Oct 03, 2013 at 07:23 PM 0
Share

Welp

At least the solution was an easy one!

avatar image BAWAZIR7 · Sep 27, 2016 at 01:06 AM 0
Share

nice answer thenk you

avatar image remibreton · Jul 01, 2017 at 11:40 PM 0
Share

Edit > Project Settings > Time did it for me.

avatar image Siggytron · Feb 14, 2019 at 02:38 PM 1
Share

If you're still following this, do you know what the fix would be now in 2019? It appears there is no longer such thing as a 'Min Penalty for Penetration' in the Project settings>Physics window.

Project Setting Physics Window

projsettingsphysics.png (73.8 kB)
avatar image
1

Answer by shreyasnisal · Oct 03, 2019 at 05:26 AM

Hi, In case anyone finds t$$anonymous$$s thread and is still looking for a solution, reducing the Default Contact Offset does the trick. You can't set t$$anonymous$$s value to 0 as with the Min Penalty for Penetration, but setting it to somet$$anonymous$$ng like 0.001 should work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to use a Raycast to see the distance traveled within a collider. 2 Answers

Move player to specific location and avoid obstacle. 2 Answers

Im trying to create a word game with a twist 0 Answers

Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers

ball sometimes goes through the bat when hit... collider problem... How to do a Raycast? 0 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