Answer the question
In order to leave comments, you need to log in
Why does the collision of the cube disappear, how can I fix it?
I am new to the unit, I wrote a code so that I could pick up a cube by pressing a button on the keyboard, but it appeared when I take it, its collision completely disappears, it goes through all the walls, how can this be fixed?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObject : MonoBehaviour
{
public GameObject obj;
private float Distance;
public float InteractDistance = 2f;
public Transform Parent;
public KeyCode TakeAnObject = KeyCode.E;
public KeyCode RemoveAnObject = KeyCode.Mouse1;
void OnMouseOver ()
{
Distance = Vector3.Distance(obj.GetComponent<Transform>().position, transform.position);
if (Distance < InteractDistance)
{
if(Input.GetKeyDown (TakeAnObject))
{
GetComponent<Rigidbody>().isKinematic = true;
transform.SetParent(Parent);
}
}
}
void Update()
{
if (Input.GetKeyDown(RemoveAnObject))
{
GetComponent<Rigidbody>().isKinematic = false;
transform.parent = null;
}
}
Answer the question
In order to leave comments, you need to log in
Pay attention to setting the value of the isKinematic field of the Rigidbody component.
https://docs.unity3d.com/ScriptReference/Rigidbody...
Solving the problem of moving an element (if the latter must interact with the environment at this time) is a very non-trivial task.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question