Hi, i’m trying to move an object based on raycast over the terrain. So it’s the typical thing.
It works fine, the object moves along the mouse, over the terrain detecting the hit. The problem is it only works over mountains. I have no idea why, when I mouse over the plains and low altitude parts of the terrain, the object gets stuck on the mountain and don’t come down.
Then if I shift the mouse from mountain to mountain the object just teleport there, without going through the plains with the mouse.
It’s like it has a height limit, but nowhere I code that. Here the code:
using UnityEngine;
using System.Collections;
public class FollowMouseOnTerrain : MonoBehaviour {
public Camera cameraToFollow; //player cam
public GameObject controllerObject; //controller object
public Terrain terrain; //terrain
private Ray _ray; //ray
private RaycastHit _hit; //hit
void Update () {
_ray = cameraToFollow.camera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(_ray.origin, _ray.direction * 10, Color.yellow); //works fine
if(Physics.Raycast(_ray.origin, _ray.direction * 10, out _hit)){ //raycast
if(_hit.collider == terrain.collider){
controllerObject.transform.position = _hit.point;
}
}
}
}