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 AmazingMrBrock · Apr 19, 2014 at 08:39 AM · physicsraycastgravityclipping

[C#] Raycast based physics and clipping

I'm working on a platforming game with raycast physics and I'm having some trouble with clipping. This is the section of code thats applying gravity

for(int i = 0; i < verticleRays; i ++){ lerpPosition = lerpPosition + lerpAmount; Vector2 origin = Vector2.Lerp(startPoint, endPoint, lerpPosition); bool clipped = true; if(origin.x == endPoint.x)origin.x = startPoint.x;

         RaycastHit2D hitInfo = Raycaster(rayLength, Vector2.up, origin, newVelocityY);
         connected = hitInfo;
         print ("Collider: " + hitInfo.collider + " Point: " + hitInfo.point + " hit distance: " + hitInfo.fraction + " connected: " + connected);
         if(!connected){
             clipped = false;
             transform.Translate(-Vector2.up * (gravity * Time.deltaTime));
             velocity = new Vector2(velocity.x, 0);
             print ("Gravity activated");
             break;
         }
         if(connected){
             Vector2 lastPosition = new Vector2(transform.position.x, transform.position.y);
             if(hitInfo.fraction > 0.2)clipped = false;
             if(hitInfo.fraction > 0){
                 transform.Translate(-Vector2.up * (gravity * Time.smoothDeltaTime));
                 velocity = new Vector2(velocity.x, 0);
                 print ("Gravity activated");
                 lastPosition = hitInfo.point;
                 break;
             }
             if(hitInfo.fraction == 0 && !clipped){
                 clipped = true;
                 Vector2 newPosition = ClipPrevent(lastPosition);
                 transform.position = newPosition;
                 break;
             }
         }
     }


as you can see I'm using a basic transform.translate with time.deltaTime. The issue I'm having is that when my character drops down it'll usually pass through the floor a variable amount. Sometimes it won't pass through the floor at all but most of the time it does.

I've been working on an anti clipping method but it doesn't seem to be working. Here's the code for that

 Vector2 ClipPrevent(Vector2 clipPoint){
         Vector2 pcClipPoint = new Vector2(transform.position.x, -boxCollider.size.y / 2);
         float clipDistance = clipPoint.y - pcClipPoint.y;
         print ("Clip Distance: " + clipDistance);
         if(clipDistance != 0){
             Vector2 newLocation = new Vector2(transform.position.x, transform.position.y - (clipDistance));
             return newLocation;
         }
         else return new Vector2(transform.position.x, transform.position.y);
     }

again as you can see its called from the previous code and returns a vector2 which is supposed to place the character at the right spot to be just touching the floor.

So yeah if anyone could help me figure out how to make objects not clip through each other I would be super happy.

Thanks in advance.

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Stop picked up object clipping through walls? 0 Answers

Cube clips and falls through wall when held 0 Answers

Held cube clips through wall 0 Answers

false gravity on a cube planet 6 Answers

Calculate where an object is going to land 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