- Home /
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();
}
}
}
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.)
you need to move a object with the rigidbody not translating it, for collision detections.
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.
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();
}
}
}
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.
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)
Your answer
Follow this Question
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