U
U
Uncle Bogdan2021-05-01 17:52:01
Unity
Uncle Bogdan, 2021-05-01 17:52:01

How to make a "smooth" movement of an object?

Now the player can move the object, but it's not smooth enough. How to make it smoother?

The code
using UnityEngine;

public class Grabbing : MonoBehaviour
{
    [SerializeField] float maxDistance;
    [SerializeField] float GrapPower;
    [Space] 
    [SerializeField] Transform Offset;

    Transform GrabObj;
    RaycastHit hit;
    bool grab;

    private void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0) || grab)
        {
            Grab();
        }
        if (Input.GetKeyDown(KeyCode.E) && grab)
        {
            Put();
        }
    }

    private void Grab()
    {
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out hit, maxDistance);
        if(grab)
        {
            GrabObj.position = Offset.position;
        }
        else if(hit.rigidbody)
        {
            grab = true;
            hit.rigidbody.useGravity = false;
            Offset.position = hit.transform.position;
            GrabObj = hit.transform;
        }
    }


    private void Put()
    {
        GrabObj.GetComponent<Rigidbody>().useGravity = true;
        grab = false;
    }
}


Video

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-05-01
@freeExec

If you are going to move RigidBody not according to physics, then you should not turn off gravity, but activate kinematic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question