Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 shgdi · Jan 28, 2019 at 03:02 PM · unity 5pickupdrop

How do I stop my objects from going through walls when I pick them up and drop them down?

When I pick an object up I can glitch it through the map making it fall out of the world. This happens when I pick it up and drop the object half way through the floor. The outcome I receive is not what I was expecting what can I do to fix this. Also yes the colliders and rigidbody's are setup correctly. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class GameManager : MonoBehaviour
 {
     public GameObject PressEtoInteractText;
 
     public bool pickup, inrange;
 
     public Collider Playercol;
 
     private Collider tempCol;
  
     public Transform guide;
 
     private GameObject temp;
 
     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.E) && inrange == true)
         {
             PressEtoInteractText.SetActive(false);
             pickup = true;
             
 
         }
 
         if (Input.GetMouseButtonDown(0) && pickup == true)
         {
           
                 temp.GetComponent<Rigidbody>().useGravity = true;
   
       
             pickup = false;
             tempCol.enabled = true;
             Playercol.isTrigger = true;
         }
 
         UpdatePickUpFollow();
        
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Interact")
         {
             PressEtoInteractText.SetActive(true);
             temp = other.gameObject;
             inrange= true;
        
         }
 
         if (other.gameObject.tag == "Interact" && temp.transform.position == guide.position)
         {
             return;
 
 
         }
 
 
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.name != "Interact")
         {
             PressEtoInteractText.SetActive(false);
             inrange = false;
         }
 
     }
 
     public void PickUp()
     {
         
         temp.GetComponent<Rigidbody>().useGravity = false;
 
        
         temp.transform.position = Vector3.MoveTowards(transform.position, guide.position, Time.deltaTime);
 
         tempCol = temp.GetComponent<Collider>();
         tempCol.enabled = false;
 
        
         temp.transform.position = guide.position;
  
         Playercol.isTrigger = false;
     }
 
     public void UpdatePickUpFollow()
     {
         if (pickup == true)
         {
             PickUp();
         }
     }
 }
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

Answer by Snipe76 · Jan 28, 2019 at 04:33 PM

I think the problem is that you set direct position right after you move the object to the guide position.

Which forces the object to be in the desired position while it ignores colliders and collision.

try removing "temp.transform.position = guide.position;"

P.S: Try avoiding GetComponent<>() in any Update functions and try to store them instead,

This will help with performance in the future because it constantly tries to get the desired component. (Imagine 1000 objects searching for a specific component. yeah... it's bad.)

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 xxmariofer · Jan 28, 2019 at 04:41 PM 0
Share

you need to move a object with the rigidbody not translating it, for collision detections.

avatar image
0

Answer by xxmariofer · Jan 28, 2019 at 04:33 PM

dont translate the object, if you want collisions you need to use the rigidbody to move the pick up object you need to use the rigidbody for moving it, try using https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html rather than vector3.movetowards.

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 shgdi · Jan 29, 2019 at 09:52 AM 0
Share

I tried what you recommended but the outcome is still the same. Could you tell me if there is something wrong with my code? { public GameObject PressEtoInteractText;

     public bool pickup, inrange;
 
     public Collider Playercol;
 
     private Collider tempCol;
 
     public Vector3 guide;
 
     private Rigidbody temp;
 
     void FixedUpdate()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && inrange == true)
         {
             PressEtoInteractText.SetActive(false);
             pickup = true;
 
 
         }
 
         if (Input.Get$$anonymous$$ouseButtonDown(0) && pickup == true)
         {
 
 
             pickup = false;
          //   tempCol.enabled = true;
             Playercol.isTrigger = true;
         }
 
         UpdatePickUpFollow();
 
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Interact")
         {
             PressEtoInteractText.SetActive(true);
             temp = other.GetComponent<Rigidbody>();
             inrange = true;
 
         }
 
         if (other.gameObject.tag == "Interact" && temp.transform.position == guide)
         {
             return;
 
 
         }
 
 
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.name != "Interact")
         {
             PressEtoInteractText.SetActive(false);
             inrange = false;
         }
 
     }
 
     public void PickUp()
     {
 
         temp.$$anonymous$$ovePosition(transform.position + guide * Time.deltaTime);
        
 
       //  tempCol = temp.GetComponent<Collider>();
        // tempCol.enabled = false;
 
 
 
 
         Playercol.isTrigger = false;
     }
 
     public void UpdatePickUpFollow()
     {
         if (pickup == true)
         {
             PickUp();
         }
     }
 }
avatar image xxmariofer shgdi · Jan 29, 2019 at 11:13 AM 0
Share

Hello, try changing the temp.$$anonymous$$ovePosition to

 temp.velocity = guide * time.deltaTime,

this will give the object a velocity in the direction that you want to move the object rather than teleporting the object to a desire position, so you maybe need to get the vector from the transform.position to the guide position. and use that

 temp.velocity = guide - transform.position; 

test both cases.

avatar image shgdi xxmariofer · Jan 29, 2019 at 11:42 AM 0
Share

I tried it and it does not work it still goes through the wall also I changed what you sent to this rb.transform.position = guide + transform.position; and it worked but still goes through the walls (What you sent was not working)

Show more comments

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

188 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

Related Questions

Picking up a box 2 Answers

UI eventsystem Raycast Unity 5 does not recognize parent and child 1 Answer

Item pickup and drop function 2 Answers

Pick up script goes straight to drop action 0 Answers

Drag 3d objects on a 3d scenario? 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