Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by gonzalitosk8 · Aug 26, 2015 at 12:46 PM · c#script.objecteffectover

Object over terrain

How I can keep an object at a height above the ground, using raycast?

something like that:

alt text

take.png (9.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Positive7 · Aug 26, 2015 at 10:09 PM

Something like this :

 using UnityEngine;
 
 public class floatObj : MonoBehaviour {
     
     public GameObject objToFloat;
     public float floatValue = 0.7f;                //0.2m + obj height /2
     //Optional solution
     //public GameObject terrain;
 
     void Update () {
         var objPos = objToFloat.transform.position;
 
         //For the Optional solution Delete from Here
         RaycastHit hit;
         if (Physics.Raycast (objPos, Vector3.down, out hit)) {
             objToFloat.transform.Translate (0, (floatValue - hit.distance), 0);
         }
         //To here. Then Uncomment below.
 
         //OR Optional
         //var terPos = terrain.transform.position;
         //objToFloat.transform.position = new Vector3 (objPos.x,terPos.y + floatValue,objPos.z);
     }
 }
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 Covenant · Aug 27, 2015 at 12:23 PM

Is this 2d or 3d game?

If it's 3d, you could cast a Ray with an origin a little above your object with Vector3.down as the direction. Then you'd call Physics.Raycast using a layermask so it'd only try to hit the terrain. Make sure you set your terrain objects to use a new layer called "Terrain" (or whatever you want to name it, just make sure the layermask has same name). If it hits, look at hit.point to get the position in the world. Then you just have to offset it so it's however higher you want. Like so:

 private Vector3 FloatObjectAboveTerrain(Vector3 objectPos)
 {
         // objectPos is position of your object
 
         // Ideally you'd set terrainMask in your Start or Awake function, 
         // no need to create this everytime this function is called
         LayerMask terrainMask = 1 << LayerMask.NameToLayer ("Terrain");
                        
         Vector3 rayOrigin = objectPos;
 
         // Raise ray origin point up some so it'll float 
         // above the object and point downwards
         rayOrigin.y += 5.0f;                
 
         Ray ray = new Ray(rayOrigin, Vector3.down);
         float distanceToCheck = 100f;
         RaycastHit hit;

         if (Physics.Raycast (ray, out hit, distanceToCheck, terrainMask))
         {
             // The ray hit an object with layer "Terrain"
 
             // hit.point is the point where the ray hit
             // The y value is how high the terrain is
             Vector3 floatingPos = hit.point;            
 
 
             // Here is where you add however much height 
             // so your object will float above the terrain
             floatingPos.y += 0.2f;                        
 
             return floatingPos;
         }
 
         // No terrain was hit by the ray, return object's original position
         return objectPos;
 }


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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Script on Prefab is not changeable without changing all other instaniated objects 1 Answer

Can we refer to two rigidbodies2d in a single script? 0 Answers

Multiple Cars not working 1 Answer

Problem with saving List of objects !!! 2 Answers


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