- Home /
mouse clicking on the plane and drag to other position
Hello, this question is quite similar to the existed questions we can serach, I have searched for many related answers, but I am still stuck.
This question is something different from others.
I have a fixed plane, plane will never change its position,it look like a map. Also there is a camera in the top view, i want to click on the plane and drag to other position. In this drag process, i want the point i click on is follow the mouse move.
I want only the camera to move, not the plane.
ps: now, i already know how to get the coordinate of point which i touch on the screen by using following code
cube=GameObject.Find("Cube");
Vector3 startpos=new Vector3();
if (Input.GetMouseButtonDown(0)){ // if click...
// create a logical plane perpendicular to Y and at Y=0:
Plane horPlane = new Plane(Vector3.up, Vector3.zero);
// get the ray from the camera...
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
print("mouseposition="+Input.mousePosition);
startpos=Input.mousePosition;
float distance1 =0;
// if the ray intersects the logical plane...
if (horPlane.Raycast(ray, out distance1)){
cube.transform.position = ray.GetPoint(distance1);
print("cube position="+cube.transform.position);
the effect i want is look like a map. Thanks in advance!!
Answer by robertbu · Jul 04, 2013 at 07:01 AM
Using a mathematical plane is a good way to go if the camera is at an angle with respect to the plane. But it reading between the lines, I get the impression that you have a map on the XZ plane at the origin of 'Y' with a camera looking directly down. With that, you can simplify things a bit and use Camera.ScreenToWorldPoint(). Example:
using UnityEngine;
using System.Collections;
public class DragMap : MonoBehaviour {
private float dist;
private Vector3 v3OrgMouse;
void Start () {
dist = transform.position.y; // Distance camera is above map
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
v3OrgMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3OrgMouse = Camera.main.ScreenToWorldPoint (v3OrgMouse);
v3OrgMouse.y = transform.position.y;
}
else if (Input.GetMouseButton (0)) {
var v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3Pos = Camera.main.ScreenToWorldPoint (v3Pos);
v3Pos.y = transform.position.y;
transform.position -= (v3Pos - v3OrgMouse);
}
}
}
Thanks for your answer, in fact the camera is not at the origin of 'Y' directly look down, it changes its position every moment, the direction it looks at has angle with the plane. In my function, i only need the camera move in a way(i can't imagine which path it should move).
I found the problem is that if i click the objectA which is exactly facing the camera lookat direcition, the drag on objectA is exactly what i want, however if i click the objectB which is besides objectA, the drag on objectB is not the right drag i expect(because objectB is not in the camera's lookat direction, B is the thing in the camera view).
Do you have some idea about this? Thanks.
It just like what you have said 'Using a mathematical plane is a good way to go if the camera is at an angle with respect to the plane.' If i understand the right meaning of your word, the camera is at an angle with respect to the plane.
Here is another version using a mathematical plane. It assume the camera always stay at the same 'Y' position with respect to the plane:
using UnityEngine;
using System.Collections;
public class Drag$$anonymous$$ap : $$anonymous$$onoBehaviour {
private float dist;
private Vector3 v3Org$$anonymous$$ouse;
private Plane plane = new Plane(Vector3.up, Vector3.zero);
void Start () {
dist = transform.position.y; // Distance camera is above map
}
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast (ray, out dist);
v3Org$$anonymous$$ouse = ray.GetPoint (dist);
v3Org$$anonymous$$ouse.y = 0;
}
else if (Input.Get$$anonymous$$ouseButton (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
plane.Raycast (ray, out dist);
Vector3 v3Pos = ray.GetPoint (dist);
v3Pos.y = 0;
transform.position -= (v3Pos - v3Org$$anonymous$$ouse);
}
}
}
@yyykobe can you set robertbu's answer as true, just tried and having great results :)