# Using raycast to bring ground plane under camera?

Hi, I have a setup where a camera can move around a scene. The user can hit a button that draws a ray to hit the ground plane in front of the camera, then the idea is that it translates the ground plane from where the ray hits to be directly underneath the camera. So this would be moving the ground, not the camera/player. Sort of like a tractor beam on the world.
I think because of parenting issues maybe the local vs. world space is messing things up. Also I may need a better reference to the direction toward the camera from the ray hit to get the right move vector. Can anyone spot the issues with this? The ground plane is not moving from the ray hit spot to underneath the camera. It seems to move according to it’s own axis.

``````IEnumerator MoveFrom()
{
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
float distanceHit;
if (plane.Raycast(ray, out distanceHit))
{
Vector3 hit = ray.GetPoint (distanceHit);
}
Vector3 vector = hit;

Vector3 lastVector = ground.transform.localPosition;
Vector3 diffVector = vector - lastVector;
Vector3 vectorNew = new Vector3(diffVector.x, lastVector.y, diffVector.z);
float t = 0;
while (t<1.0f)
{
t += Time.deltaTime/time;
ground.localPosition = Vector3.MoveTowards(lastVector, vectorNew,  t);
yield return null;
}
}
``````

Here is a bit of code that worked fine for me in light testing. It use world positions, so it will not matter if the ground is parented or not. I move the ground plane at a fixed speed. You can go back to a fixed time movement if that is what you want for your game. It assumes the ground has a ‘y’ value of 0.0.

``````using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {

public Transform ground;
public float speed = 4.5f;

void Update() {
if (Input.GetKeyDown(KeyCode.Space))
StartCoroutine(MoveFrom());
}

IEnumerator MoveFrom()
{
Vector3 pos = transform.position;
pos.y = 0.0f;
Plane plane = new Plane(Vector3.up, pos);
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
float distanceHit;
Vector3 vector = pos;
if (plane.Raycast(ray, out distanceHit))
{
vector = ray.GetPoint (distanceHit);
}

Vector3 newPosition = ground.position - (vector - pos);

while (ground.position != newPosition)
{
ground.position = Vector3.MoveTowards(ground.position, newPosition,  speed * Time.deltaTime);
yield return null;
}
}
}
``````