Need to round to stop object shaking

I’m having a problem where I’m casting a ray against something and it is causing it to shake along the X and Z axis (Y is a set # so it is fine). I looked at the position coords and they are changing by like plus or minus .01 constantly, which is causing the jitter. I need to figure out how to either just drop the .01 off completely or stop it from changing so much… Here is the code I’m using to get this info.

void OnMouseDrag()
    {
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        Physics.Raycast(ray, out hit);
        Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
        transform.position = new Vector3(hit.point.x, 5.1f, hit.point.z);
    }

This is for a drag-able object fyi (if you couldn’t tell from the code heh).

Thanks!

anyone? I really don’t know how to fix the jittering. Is there any way to clamp the number to cut off decimals or just a way to get it to stop jittering via different uses of the ray, colliders, something else entirely?

Perhaps the DragRigidbody script from the Standard Assets would work for your purposes?

Also, you can have a look at this DragObject script, which is quite stable: unifycommunity.com

Also see this discussion on UnityAnswers, it mentions a drag object script from the Shadows demo: http://answers.unity3d.com/questions/4898/drag-gameobject-with-mouse

Thanks. I used that wiki version (tho I ported it to C# real quick). It worked like a charm. Surprised so much code has to go into a drag and drop mechanic… I was able to get a crappy one in like 6 lines of code heh.

I knew about the standard asset script, but I heard that it was very unstable and hard to use out of the box, so I wanted to avoid it.

Thanks again though. I’ve been wracking my brain for the last few hours trying to fix it and couldn’t figure it out. This was much, much easier =).

Here’s the C# Port for anyone who cares/stumbles upon this:

using UnityEngine;
using System.Collections;

public class DragObject : MonoBehaviour {

public int normalCollisionCount = 1;
public float moveLimit = .5f;
public float collisionMoveFactor = .01f;
public float addHeightWhenClicked = 0.5f;
public bool freezeRotationOnDrag = true;
private Camera cam;
private Rigidbody myRigidbody;
private Transform myTransform;
private bool canMove = false;
private float yPos;
private bool gravitySetting;
private bool freezeRotationSetting;
private float sqrMoveLimit;
private int collisionCount = 0;
private Transform camTransform;

void Start () {
    myRigidbody = rigidbody;
    myTransform = transform;
    if (!cam) {
        cam = Camera.main;
    }
    if (!cam) {
        Debug.LogError("Can't find camera tagged MainCamera");
        return;
    }
    camTransform = cam.transform;
    sqrMoveLimit = moveLimit * moveLimit;   // Since we're using sqrMagnitude, which is faster than magnitude
}

void OnMouseDown () {
    canMove = true;
    myTransform.Translate(Vector3.up*addHeightWhenClicked);
    gravitySetting = myRigidbody.useGravity;
    freezeRotationSetting = myRigidbody.freezeRotation;
    myRigidbody.useGravity = false;
    myRigidbody.freezeRotation = freezeRotationOnDrag;
    yPos = myTransform.position.y;
}

void OnMouseUp () {
    canMove = false;
    myRigidbody.useGravity = gravitySetting;
    myRigidbody.freezeRotation = freezeRotationSetting;
    if (!myRigidbody.useGravity) {
        myTransform.position = new Vector3(myTransform.position.x, yPos - addHeightWhenClicked, myTransform.position.z);
    }
}

void OnCollisionEnter () {
    collisionCount++;
}

void OnCollisionExit () {
    collisionCount--;
}

void FixedUpdate () {
    if (!canMove) return;
    
    myRigidbody.velocity = Vector3.zero;
    myRigidbody.angularVelocity = Vector3.zero;
    myTransform.position = new Vector3(myTransform.position.x,yPos,myTransform.position.z);
    Vector3 mousePos = Input.mousePosition;
    Vector3 move = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
    move.y = 0.0f;
    if (collisionCount > normalCollisionCount) {
        move = move.normalized*collisionMoveFactor;
    }
    else if (move.sqrMagnitude > sqrMoveLimit) {
        move = move.normalized*moveLimit;
    }
    
    myRigidbody.MovePosition(myRigidbody.position + move);
}
}