- Home /
Cube clips and falls through wall when held
Hello im making a game where i need to pickup a cube and throw it away but i ran into a problem. When i hold the cube and look into a wall or ground it clips through it i have a video here: https://imgur.com/a/qu3PZ8F and here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pickup : MonoBehaviour
{
public Camera cameraa;
public bool pickedup;
public Rigidbody cube;
public GameObject cubeobj;
public Collider objcoll;
public Collider playercoll;
public int push;
void Update()
{
if (Input.GetMouseButtonDown(0) && pickedup == false)
{
RaycastHit hit;
Ray ray = cameraa.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Transform objectHit = hit.transform;
if (objectHit.tag == "liftable")
{
Debug.Log("Hit Liftable");
cubeobj.transform.parent = cameraa.transform;
Physics.IgnoreLayerCollision(9, 8); // Ignores collision with player
cube.useGravity = false; //Turns of gravity
cube.constraints = RigidbodyConstraints.FreezeAll; //Freezes movement and rotation
pickedup = true;
}
}
}
if (Input.GetMouseButtonDown(1) && pickedup == true)
{
//throw away
Debug.Log("throw");
cube.constraints = RigidbodyConstraints.None;
cube.useGravity = true;
cube.transform.parent = null;
Physics.IgnoreLayerCollision(8, 9, false);
cube.AddForce(transform.forward * push);
pickedup = false;
}
}
}
Comment