Im new in Unity and i want to program a simple game. I want to transport a Object to the position where i clicked, but only on the x-Axis.
As Example, i click on point 4,4,0 , i want that the Object transports, not moves, to point 4,0,0.
You need some kind of ground, like a flat plane which has a collider, and an object like a cube.
public GameObject go;//Drag your Object here
void Update() {
//first, we define a Ray. It will take the current mouse position on screen and convert it into world space. Basically, the ray will start near the camera and go straight forward.
Ray mouseRay = Camera.main.GetComponent<Camera>().ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(mouseRay, out hit, Mathf.Infinity) == true) {
Vector3 targetPos = new Vector3(hit.point.x, 0, 0);//note that the x value of the new Vector3 is equal to the x value of the position the raycast hit (hit.point), but the y and z values are left to 0.
go.transform.position = targetPos;//pass the newly calculated position the the gameobject's Transform.
}
}