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 Kennyist · Nov 19, 2013 at 06:44 AM · c#collisionrigidbodytransform

Holding objects, How to deal with object clipping

I'm working on a object holding script and have this so far (WASD, mouse click to pickup and drop)

http://kennyist.com/unity/2playergame/foranswers.html

But on this, You can clip objects through walls also it seems generally laggy. How would you go about stopping this from happening.

The script:

 public class PickUpObjects : MonoBehaviour {
 
 public Camera camera;
 private RaycastHit hit;
 bool carry = false;

 void FixedUpdate () {
     if(Input.GetMouseButtonUp(0)){
         if(carry){
             
             carry = false;
             hit.rigidbody.useGravity = true;
             hit = new RaycastHit();
             
         } else {
             
             if(Input.mousePosition.x < Screen.width / 2){
                 Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 100f);
                 Debug.Log(hit.transform.gameObject.name);
                 
                 if(Vector3.Distance(transform.position, hit.transform.gameObject.transform.position) < 1){
                     hit.rigidbody.useGravity = false;
                     carry = true;
                 }
             }
         }
     }
     
     if(carry){
         Carry();    
     }
     
 }
 
 void Carry(){
     Rigidbody hitRidg = hit.transform.gameObject.rigidbody;
     
     hitRidg.MoveRotation(transform.rotation);
     
     Vector3 newPos = new Vector3(transform.position.x,transform.position.y + 0.2f,transform.position.z);
     hitRidg.MovePosition(newPos + transform.forward * 0.2f);
     
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by s_guy · Nov 20, 2013 at 01:16 AM

Lots of big budget games let weapons and carried objects clip through walls, but I agree it doesn't look great.

One way to do this is to have the walls push the object aside as you move up against them:

  • Put the object you are holding as a sibling (not a child) to the rigidbody player controller, with a common non-rigidbody parent.

  • Make the object you are holding a rigidbody if it is not one already.

  • Make a physics layer that interacts with the environment, but not the player. Put the carried object on this layer.

  • Attach the carried object to the player by a joint with the desired properties, e.g. a spring joint, or construct a custom (or purchase an) IK apparatus for the arms. Adjust settings such that the carried object tends not to intersect the player in undesirable ways.

...or if you're ok with the player not being able to get as close to walls when carrying something, set the carried object up as a compound collider by making it have a collider and be a child of the player's rigidbody. See "Compound Colliders" at the following location.

http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html

Also, if you want the carried object to just snap / teleport to the carried position, you might try setting its transform directly instead of MovePosition, which may or may not succeed if there is a static obstacle in the way.

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 $$anonymous$$ · Aug 05, 2016 at 03:14 PM 0
Share

It doesnt work for me

avatar image
0

Answer by Cherno · Nov 19, 2013 at 09:22 AM

You can use a second camera that only renders the things you are holding, and set this camera's rendering depth to be below the one from your main camera.

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 Kennyist · Nov 20, 2013 at 12:22 AM 0
Share

Sorry, I meant with collisions. When holding an object you can just push it through the wall, Where as normal collisions 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

18 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

Related Questions

Collision detection from specific directions using rigid body character? 0 Answers

Objects with colliders going through walls and each other. 3 Answers

Prevent a specific RB to influence another, but still collide with everything 0 Answers

Force a script generated primitive to test for collision 2 Answers

Moving object with transform.position ignore other objects even if they collided 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