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!!
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);
}
}
}