x


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;

         }

       }

    }
}
more ▼

asked Jun 06 '12 at 12:03 PM

SuIXo3 gravatar image

SuIXo3
36 6 11 15

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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>";
more ▼

answered Jun 06 '12 at 02:11 PM

Owen Reynolds gravatar image

Owen Reynolds
11.3k 1 7 45

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 06 '12 at 05:09 PM

SuIXo3 gravatar image

SuIXo3
36 6 11 15

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 06 '12 at 12:22 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

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.

Jun 06 '12 at 12:40 PM SuIXo3

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.

Jun 06 '12 at 12:49 PM SuIXo3

To be precise it stays at 1.5 units from the ground (0).

Jun 06 '12 at 12:51 PM SuIXo3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x1528
x1471
x1279
x983

asked: Jun 06 '12 at 12:03 PM

Seen: 534 times

Last Updated: Jun 06 '12 at 05:09 PM