- Home /
Dragging a 3D object and limiting its Y position
I'm using this script that I found/modified to move a 3d object by clicking and dragging it. It works but I have one major flaw: I want to ensure that it never moves along the Y axis, only the X and Z. My game is an isometric 3D grid style game.
Here is my code. As you can see I will always set the Y to 2f as I don't want the concept of up/down, just left/right. The issue this creates is that after moving a few units, my mouse and the object I am dragging will no longer be together. I believe this is because my mouse is still moving in the Y plane. What I would like to resolve is that the cursor and game object being dragged remain in unison.
using UnityEngine;
using System.Collections;
public class Drag : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
/**
* Mouse is cilcked
*/
void OnMouseDown() {
Vector3 mouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(mouse);
}
/**
* Mouse is dragged
*/
void OnMouseDrag() {
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
curPosition.y = 2f;
transform.position = curPosition;
}
}
You can freeze it's position from the inspector in the Y axys
Answer by maccabbe · Apr 12, 2015 at 09:53 PM
It seems like this problem would be solved by using a better method of getting the target position. Instead of using Camera.ScreenToWorldPoint you should instead use Camera.ScreenPointToRay to get a ray then find where the ray intersects with the plane y=2. For example
void Update(){
Plane plane=new Plane(Vector3.up,new Vector3(0, 2, 0));
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
float distance;
if(plane.Raycast(ray, out distance)) {
transform.position=ray.GetPoint(distance);
}
}
Incredible, thank you. I don't understand the Plane though.
Create a plane, which acts like a divider/boundary I'm guessing
Create a Ray, easy enough
Float distance is = to nothing, not sure I understand
Don't understand the if statement. What makes plane.Raycast true?
The Unity documentation does a good job explaining how Plane.Raycast work http://docs.unity3d.com/ScriptReference/Plane.Raycast.html
3) Plane.Raycast uses "out distance" as one of the arguments. It means that distance does not need to be defined before being passed and in this case is assigned the distance along the ray by Plane.Raycast. See the c# documentation for more details https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx.
4) Plane.Raycast is true if the ray intersects with the plane.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObject : $$anonymous$$onoBehaviour
{
public float yOffsetForDraggedObject = 1;
Plane plane;
float distance;
private void Start()
{
plane = new Plane(Vector3.up, new Vector3(0, yOffsetForDraggedObject, 0));
}
void On$$anonymous$$ouseDrag()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
transform.position = ray.GetPoint(distance);
}
}
}
Here's essentially the same code, but the entire script, reformatted for efficiency. I changed Update() to On$$anonymous$$ouseDrag() to pick up the object when it's clicked.
Your answer
Follow this Question
Related Questions
Following instruction for drag and drop 3d object unity but not working 1 Answer
spawn and drag game object without leaving mouse button 1 Answer
Drag and drop picks an object too far away from mouse position 2 Answers
Collision while dragging in Canvas with IDragHandler 0 Answers
Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position 0 Answers