A
A
aska3222021-03-04 13:19:08
Unity
aska322, 2021-03-04 13:19:08

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?6040b35e0dc6e750402353.png

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

1 answer(s)
P
pashara, 2021-03-04
@pashara

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 question

Ask a Question

731 491 924 answers to any question