- Home /
Raycast moved object won't go under certain height
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;
}
}
}
}
Answer by aldonaletto · Jun 06, 2012 at 12:22 PM
This code should work. Looks like you have a kind of invisible plane that's blocking the raycast before it hits the lower parts of the terrain. You could change the inner if like this:
... if(Physics.Raycast(_ray.origin, _ray.direction * 10, out _hit)){ //raycast if(_hit.collider != controllerObject.collider){ controllerObject.transform.position = _hit.point; } } ...This could help understanding what's actually happening.
NOTE: You could use only Physics.Raycast(ray, out hit) - it's a little faster, since there are less parameters to pass.
Ok many thanks, I'll try. BTW, there is no plane near these areas but the water plane which is just under the terrain, less than 1 unit under it. I tried to deactivate colliders around but no changes.
Ok it works, it follows the mouse. But the height is unchanged. Before it didn't go out the mountain, now it does, but remains at the same height as the lower available position on the mountain. You see? It's like there is an invisible plane just over the terrain, and I can see how the sphere(the gameobject) don't penetrate the ground as it should(pivot is in center).
So for what I want for now is enough, because it's a controller to get a position on XZ, but if I want i.e. to make a shadow for building construction, it won't be good to have it floating over the terrain.
Thanks anyway, I now can make what I wanted.
Answer by Owen-Reynolds · Jun 06, 2012 at 02:11 PM
For a test, print out what/when it hits to something you can see in the Inspector. Maybe it's hitting something that blocks it, or missing(?) somehow:
public string hName; // debugging global variable
...
if( raycast ) { hName=_hit.transform.name;
...
} else hName="<missed>";
Answer by SuIXo3 · Jun 06, 2012 at 05:09 PM
AAAAAAAAAA I just found out what it was. Super dumb thing. I forgot that the outer perimeter of the map had the collider on, and it's a bit over the play map.
Works nice now perfect. Thanks for help!