- Home /
Dragging an object smoothly
Hi,
With the current code below, I can drag and snap an object but the movement is jerky as it calculates round coordinates to snap objects together. How can I make the dragging smoother ? Thanks !
Here's the code :
void OnMouseDown()
{
scanPos = gameObject.transform.position;
screenPoint = Camera.main.WorldToScreenPoint(scanPos);
offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.x = (float)(Mathf.Round(curPosition.x / gridSize_x) * gridSize_x);
curPosition.y = (float)(Mathf.Round(curPosition.y / gridSize_y) * gridSize_y);
curPosition.z = (float)(Mathf.Round(curPosition.z / gridSize_z) * gridSize_z);
transform.position = curPosition;
}
What I did so far :
I can smoothly move the object around and snap it with another object if I press right mouse click. What I'd like to do is snap it automatically without user input.
void On$$anonymous$$ouseDrag()
{
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
//Snap mode ON
if (Input.Get$$anonymous$$ouseButton(1)) {
curPosition.x = (float)($$anonymous$$athf.Round(curPosition.x / gridSize_x) * gridSize_x);
curPosition.y = (float)($$anonymous$$athf.Round(curPosition.y / gridSize_y) * gridSize_y);
curPosition.z = (float)($$anonymous$$athf.Round(curPosition.z / gridSize_z) * gridSize_z);
transform.position = curPosition;
}
}
Answer by Mmmpies · Feb 21, 2015 at 05:53 PM
Don't know if this helps but I put together a basic Kerbal Space Program test a while ago. Scripts are in the package:
Might be some unused scripts in there as well and as ever, it was OK when it left me but virus check it as it's been on the Internet.
EDIT
Ah OK, the DragWithMouse script is years old, it doesn't do snapping but will drag something smoothly.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshCollider))]
public class DragWithMouse : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
public bool IsDragable = true;
void OnMouseDown()
{
if(IsDragable) // Only do if IsDraggable == true
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseDrag()
{
if(IsDragable) // Only do if IsDraggable == true
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
}
Thanks I've checked it but I got loads of errors so I removed it. But thanks anyway :)
Odd I opened it into a new project and got no errors at all. Opened the scene and dragged the objects around to make sure it worked before posting.
I'm probably using an older version of Unity without the new UI system. That's why I'm getting those errors. But I've managed to open the scene and it looks really good. Thanks for sharing this :)
I converted that to comment as it doesn't appear to be an answer, I hope that's O$$anonymous$$. Right I think I've taken out everything that would have caused errors so try this:
It uses offset colliders, when something snaps to the bottom it moves the collider to below that item.
It still gives me errors when I snap the second object to the third, etc.
Answer by Khoray · Apr 22, 2017 at 10:07 PM
You can try this also
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObject : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown(){
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag(){
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Raycast Snapping Problem. 0 Answers
Multiple Cars not working 1 Answer
Item pickup and drop function 2 Answers
Snap object rotation 1 Answer