- Home /
Box wont collide with anything when picked up
Hello there, I'm making a Box pick up script and I'm having some issues with making the box collide with stuff. ( https://gyazo.com/21ddc772244ee938765d08a677df9c93 ) I want it to collide with the walls and other boxes. my code:
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEditor.Timeline;
using UnityEngine;
public class BoxScript : MonoBehaviour
{
public Transform Box;
public Transform Player;
public Transform BoxPosition;
public Transform Map;
public bool isHolding = false;
public int maxDist = 5;
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
if (isHolding == false)
{
if (Vector3.Distance(Player.transform.position, Box.transform.position) < maxDist)
{
isHolding = true;
Debug.Log("Grab");
Box.GetComponent<Rigidbody>().isKinematic = true;
Box.position = BoxPosition.position;
Box.parent = BoxPosition;
}
}
}
}
private void Update()
{
if (isHolding == true)
{
if (Input.GetMouseButtonUp(0))
{
isHolding = false;
Debug.Log("Drop");
Box.GetComponent<Rigidbody>().isKinematic = false;
Box.parent = Map;
}
}
}
}
Answer by Rasmus_Bonnesen · Aug 09, 2020 at 02:55 PM
Well I don't exactly know why this is happening(my best guess is that it has something with your player controller script to do), but I have a workaround to not make it visible that the cube clips through walls for the player.
1) Make a new Layer, and call it pickedUpCube or soemthing like that. 2)In your pickup script assign the layer to the cube everytime you pick up the cube.
int idOfPickedUpLayer;
Box.gameObject.layer = idOfPickedUpLaye
And when you drop the cube again, make sure the layer gets set back to whatever it was before.
Box.gameObject.layer = 0;
3) On your main Player camera remove the "pickedUpCube" layer from its culling mask.
4) Add a new camera give it the same position your main player camera has, and make it a child object of it.
5) Set the Clear Flags value of the new camera to Depth only, and set its Culling Mask to the "pickedUpCube" layer, also make sure to remove all the othere layers from its culling mask.
If it doesn't work, try changin your Depth value on your main camera to 0 and 1 on your new camera.
Now the cube will probably still clip through walls but it won't be noticeable for the player anymore.
Hope this helps :)
Your answer
Follow this Question
Related Questions
Rigidbody set velocity on 1 axis without changing the others. 5 Answers
Need help In creating android game with UI buttons ?Please Help 1 Answer
Scripts not updating the material's shader properties 1 Answer
Flip between 2 states constantly C#? 1 Answer
In-Game Tutorial with special actions in special areas. (Shooter) 0 Answers