Hi,
If I drag, then the object is jumping to muse cursor center on the Z axis.
I’d like to add offset to this script, because I don’t need “jumping”.
Any idea will be helpful.
using UnityEngine;
using System.Collections;
public class drag_v12 : MonoBehaviour
{
private float distance;
void OnMouseDrag()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen ));
transform.position = new Vector3( pos_move.x, transform.position.y, pos_move.z );
}
}
You need to find the distance between the object position and the position of the mouse when you clicked on the object.
In “OnMouseDown ()” you compare the two positions in “world space position”.
When you move the object, you have to subtract the distance that he was of the click.
using UnityEngine;
using System.Collections;
public class drag_v12 : MonoBehaviour
{
private Vector3 distance;
void OnMouseDown()
{
distance = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,0)) - transform.position;
}
void OnMouseDrag()
{
Vector3 distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen.z ));
transform.position = new Vector3( pos_move.x - distance.x , transform.position.y, pos_move.z );
}
}
3 Likes
Thanks, I have tried your code, but the objects are jumping on the X axis and also jumping to mouse center.
If the object is on the left side then jumping left.
If the object is on the right side then jumping right.
Please try my project and you will see what is my problem.
http://terberendezo.alexbutor.hu
user: a
pass: a
Really, the code I sent you does not work. I tested in a 2D project and had worked.
The script only needs some tweaking to work as expected.
using UnityEngine;
using System.Collections;
public class drag_v12 : MonoBehaviour
{
private Vector3 distance;
void OnMouseDown()
{
distance = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(transform.position).z)) - transform.position;
}
void OnMouseDrag()
{
Vector3 distance_to_screen = Camera.main.WorldToScreenPoint(transform.position);
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen.z ));
transform.position = new Vector3( pos_move.x - distance.x , transform.position.y, pos_move.z -distance.z );
}
}
3 Likes
Hi,
I give you the 8th star, because your code is working well!
I have only 2 more questions:
If you check again my project, then you will see your code with some additions.
I’d like to make dragging objects without pass through on each other.
I can drag
If triggered then dragging is stopping
If the object are stacked then the dragged object is jumping back to the first trigged position.
Problem: Under dragging I can’t transform back one, therefor you will see they are passing through on each other.
I have read somewhere that the fast moving objects passing through on each other.
If I’m dragging very fast an object and trying stuck out each other, then I can.
If you have any idea, you would be very helpful.
```csharp
**using System.Collections;
using UnityEngine;
class drag_mouse : MonoBehaviour
{
private bool Triggered = false;
private bool dragging = false;
private Vector3 distance;
private Vector3 scanPos;
void OnMouseDown()
{
distance = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(transform.position).z)) - transform.position;
dragging = true;
}
void OnMouseUp()
{
dragging = false;
if (Triggered)
{
transform.position = scanPos;
Debug.Log(name + " Vissza az érintkezési ponthoz");
}
}
void Update()
{
if (dragging)
{
if(Input.GetKeyDown(KeyCode.A))
transform.Rotate(0, 90, 0);
if(Input.GetKeyDown(KeyCode.C))
Destroy (gameObject);
if(Input.GetKeyDown(KeyCode.D))
Instantiate (gameObject);
Vector3 distance_to_screen = Camera.main.WorldToScreenPoint(transform.position);
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen.z ));
transform.position = new Vector3( pos_move.x - distance.x , transform.position.y, pos_move.z -distance.z );
}
}
void OnTriggerEnter (Collider myTrigger)
{
if(myTrigger.gameObject.tag == "Player")
{
dragging = false;
scanPos = gameObject.transform.position;
Triggered = true;
Debug.Log(name + " Triggered");
}
}
void OnTriggerExit (Collider myTrigger)
{
if(myTrigger.gameObject.tag == "Player")
{
Debug.Log(name + " Snap feloldása Drag alatt");
Triggered = false;
}
}
}**
```
You can try to disable isTrigger when you release the mouse button. This will make objects react to collision and they will not be stacked.
The problem is that when you’re dragging, they still will cross trough the other.
Exemple:
https://dl.dropboxusercontent.com/u/29523611/Help/Help_Build.html
That’s the best I can do for now.
using UnityEngine;
using System.Collections;
public class drag_v12 : MonoBehaviour
{
private Vector3 distance;
private bool canDrag;
private Rigidbody rb;
private BoxCollider bc;
private Renderer rend;
void Start()
{
rb = GetComponent<Rigidbody> ();
rb.freezeRotation = true;
rb.mass = 1;
bc = GetComponent<BoxCollider> ();
bc.isTrigger = false;
rend = GetComponent<Renderer> ();
}
void Update()
{
if (canDrag)
{
Vector3 distance_to_screen = Camera.main.WorldToScreenPoint (transform.position);
Vector3 pos_move = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance_to_screen.z));
transform.position = new Vector3 (pos_move.x - distance.x, transform.position.y, pos_move.z - distance.z);
rend.material.color = new Color(1,1,1,0.5f);
}
}
void OnMouseDown()
{
bc.isTrigger = true; // Not react to collisions
canDrag = true;
rb.mass = 0; // become weightless, not to move other objects
distance = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(transform.position).z)) - transform.position;
}
void OnMouseUp()
{
canDrag = false;
bc.isTrigger = false; // React to collisions
Invoke ("ResetMass", 0.1f);
}
private void ResetMass()
{
rb.mass = 1;
rend.material.color = new Color(1,1,1,1);
}
}
Thank you so much, you helped a lot.
Your idea and demo is perfect, I think this will help for a lot of people.
The first drag script was posted 5 year ago from someone and now you have updated to better in 2015!
Unfortunately this isn’t working with Non-convex object, but you have opened my mind.
Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
If you want to use a non-convex mesh either make the Rigidbody kinematic or remove the Rigidbody component. Scene hierarchy path “AS-8.6-2A-4Ü/polySurface39”, Mesh asset path “Assets/furnitures/AS_8_6_2A_4U_FBX.fbx” Mesh name “polySurface39”
Modified post: with the tables is working well, but whit cabinet I get the error. I need to find the differences between my 3D models.
I have seen your Time of Day & Dynamic Skybox project and very beautiful.
/Let me Help Remember a small mistake. If I click your signature azure[sky] Dynamic Skybox , then on the top of the post is Dinamic !=Dynamic./
Thanks everything again my friend.