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

Colliders overlapping

I feel like this question MUST have been answered somewhere before but after a few hours of research I can't find anything 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 hits 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 hit;
     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 hit, length))
             {
                 if (hit.distance < 0.5f)
                 transform.position = transform.position + (Vector3.up*(0.5f - hit.distance));
             }
             }
     //When the ray hits an object, check and see if the length between the ray's origin and
     //the hit 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.

This logic seems sound to me but this 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 hits 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 this list to address this 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 '$$anonymous$$in 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 this thread and is still looking for a solution, reducing the Default Contact Offset does the trick. You can't set this value to 0 as with the Min Penalty for Penetration, but setting it to something 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.

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

Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers

Raycast, Linecast, sphere collider or capsule collider? 0 Answers

Physics.Raycast not checking layermask properly? 1 Answer

methods for mouse picking that do not use physics? 1 Answer

Using GetPixel via Raycasting Without Colliders 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