Answer the question
In order to leave comments, you need to log in
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?
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;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question