K
K
KaRaMiD2021-08-05 01:27:41
3D
KaRaMiD, 2021-08-05 01:27:41

How to properly pick up and move objects in Unity 3D?

I have a problem when I put an object under my feet, then the character and the box starts moving back and forth.
610b13c2a57e8644050541.png
And I want to fix it, I had an idea to limit the movement of the object to Y, but I don't know how to do it.
And I also don't know how to make picking up and releasing an object by pressing a button.

The script is on the box

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TakeControll : MonoBehaviour
{
    [Header("For take and throw")]
    private bool isTaken = false;
    private bool canHold = true;
    public float throwForce = 200f;
    public float distance;

    private Vector3 objPos;
    public GameObject DotForTake;
    public Transform DotForTake1;
    public Vector3 DotForTake2;

    [Header("For normaly take obj.y")]
    private Vector3 testForNormalTake;

    void Start()
    {
        DotForTake2 = DotForTake1.transform.position;
        testForNormalTake.y = DotForTake2.y;
    }

    void FixedUpdate()
    {
        distance = Vector3.Distance(this.transform.position, DotForTake.transform.position);
        if (distance >= 1f)
        {
            isTaken = false;
        }
        if (isTaken == true)
        {
            this.transform.Rotate(new Vector3(0, 0, 0));
            this.GetComponent<Rigidbody>().velocity = Vector3.zero;
            this.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
            this.transform.position = DotForTake1.position;

            if (Input.GetMouseButtonDown(0))
            {
                //throw
                this.GetComponent<Rigidbody>().AddForce(DotForTake.transform.forward * throwForce);
                isTaken = false;
            }

            
        }
        else
        {
            objPos = this.transform.position;
            this.transform.SetParent(null);
            this.GetComponent<Rigidbody>().useGravity = true;
            this.transform.position = objPos;
        }
    }

    void OnMouseOver()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
                if (distance <= 1f)
                {
                    isTaken = true;
                    this.GetComponent<Rigidbody>().useGravity = false;
                    this.GetComponent<Rigidbody>().detectCollisions = true;
                }
            }
            if (Input.GetKeyUp(KeyCode.E))
            {
                isTaken = false;
            }
        }
}

DotForTake, DotForTake1, DotForTake2 is the point where the object weighs when isTaken = true
610b13a9079dc474605295.png

Please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nekit Medvedev, 2021-08-09
@NIKROTOS

hang the trigger on the cube (larger than the cube) and if the cube intersects with the dragging one, then do not move towards yourself. The trigger can be done at the moment of taking the object.
In the book unity in action at the beginning, when describing how to work with the mouse, it is described how to implement a similar restriction: p. 49
https://www.rulit.me/data/programs/resources/pdf/H...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question