Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SuIXo3 · Jun 06, 2012 at 12:03 PM · gameobjectraycasttransformterrainmouse

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;
                 
             }
             
         }
             
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuIXo3 · Jun 06, 2012 at 12:40 PM 0
Share

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.

avatar image SuIXo3 · Jun 06, 2012 at 12:49 PM 0
Share

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.

avatar image SuIXo3 · Jun 06, 2012 at 12:51 PM 0
Share

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

avatar image
0

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>";
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiated Objects not being set at ground/terrain level?(Solved) 1 Answer

use list of gameobjects transform in raycast 0 Answers

Shaking GameObject Problem 1 Answer

Ray camera to Terrain goes Wrong. 1 Answer

Game Object flies out of screen when dragged 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges