How to negate shaking of moving object?
I create a simple "drag and drop" script that moves selected rigidbody with mouse. It works just fine if i use orthographic camera projection, but if i use perspective camera projection, selected odject starts to shake.
There is gif demonstration of this problem (it is too large, so i post it on Google Drive) and also all of my code, if it is needed :
problem demonstration
public class DragAndDrop : MonoBehaviour
{
private Collider target;
private bool targetAquired;
void Awake()
{
targetAquired = false;
}
private void Update()
{
if (!targetAquired)
{
if (Input.GetMouseButton(0))
{
if (!targetAquired){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.GetComponent<Rigidbody>() != null)
{
targetAquired = true;
target = hit.collider;
}
}
}
}
}
else
{
TargetHandler(target);
}
}
private void TargetHandler(Collider _target)
{
Rigidbody targetRigidbody = _target.GetComponent<Rigidbody>();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
targetRigidbody.MovePosition(hit.point);
}
if (Input.GetMouseButtonUp(0)){
targetAquired = false;
}
}
}
Answer by migrus2013 · Apr 16, 2019 at 07:39 PM
So.... Nobody answered this question, so i'll transform this topic to my own discoveries. First of all i created a plane, that will recieve touches from camera rayhits. It's worked, but also crated strange offset between mouse and object
Now i'm stuck with this and i'll try to make it work like i want later
This is the last post. There are all my problems explained in as many details as I can provide. So shaking was caused by raycast cast at plane and gameobject alternately, and gameobject teleports or to his position, or to plane hit's position. Strange offset was caused by my object's fixed position at Z axis. And also there is my c# final code if anyone needs it.
public class DragAndDrop : $$anonymous$$onoBehaviour
{
private Collider target;
private bool targetAquired;
private Vector3 offset1;
public GameObject touchPlane;
void Awake()
{
targetAquired = false;
touchPlane.SetActive(false);
offset1 = new Vector3(0.0f, 0.5f, 0.0f);
}
private void Update()
{
if (!targetAquired)
{
if (Input.Get$$anonymous$$ouseButton(0))
{
if (!targetAquired){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.GetComponent<Rigidbody>() != null)
{
targetAquired = true;
target = hit.collider;
}
}
}
}
}
else
{
TargetHandler(target);
}
}
private void TargetHandler(Collider _target)
{
touchPlane.SetActive(true);
Rigidbody _targetRigidbody = _target.GetComponent<Rigidbody>();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
_targetRigidbody.$$anonymous$$ovePosition(hit.point - offset1);
}
if (Input.Get$$anonymous$$ouseButtonUp(0)){
targetAquired = false;
touchPlane.SetActive(false);
}
}
}