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
0
Question by samualsock · Mar 15, 2015 at 07:39 PM · movementraycastingmouse control

Trying to keep an object 1.5 above the terrain at all times

I am working on unit movement for an RTS and I want to be able to keep my unit a set distance above the terrain which varies in height with hills and such. I was using a raycast from the bottom of the unit and then adjusting the height based on that. Below is the coroutine from where the function containing the height check is called:

 IEnumerator Move(GameObject Unit, float targetX, float targetZ, float targetY) 
     {
         Vector3 clickedPosition = new Vector3 (targetX, targetY + 2.5f, targetZ);
         Vector3 startPosition = transform.position;
         while(Vector3.Distance(startPosition, clickedPosition)>1f) 
         {
             transform.position=Vector3.MoveTowards(transform.position, clickedPosition, 6 * Time.deltaTime);
             HeightCheck();
             yield return null;
         }
         Debug.Log("The object has reached the destination");
     }

And here is the code for HeightCheck void HeightCheck () {

         RaycastHit hit = new RaycastHit();
         
         if (Physics.Raycast (transform.position, Vector3.down, Mathf.Infinity))
         {
             Vector3 tempPos = transform.position;
             if(hit.distance > 1.5)
             {
                 //float aboveOnePointFive = transform.position.y - hit.distance + 2.5f;
                 tempPos.y = transform.position.y - hit.distance + 2.5f;
                 Debug.Log("The distance is not right from the ground");;
             }
 
             if(hit.distance < 1.5)
             {
                 //float belowOnePointFive = transform.position.y - hit.distance + 2.5f;
                 tempPos.y = transform.position.y - hit.distance + 2.5f;
                 Debug.Log("The distance is not right from the ground");;
             }
         }
     }

This isn't working and I am unsure of why. I am also unsure if this is the most efficient way to go about doing something like this. Any help is much appreciated :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by siaran · Mar 15, 2015 at 09:11 PM

Are you using Unity's built in terrain? In that case, you may want to make use of the SampleHeight function and do something like this

 transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position) + 1.5f;


I have no idea how efficient that method is, though.

Comment
Add comment · Show 1 · 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 samualsock · Mar 15, 2015 at 09:18 PM 0
Share

I'm not using the in built terrain, I have my own model :/

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Click Mouse anywhere for different actions 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Raycast only works right next(practically on top of) object 0 Answers

RTS movement not working properly 1 Answer

Making a bubble level (not a game but work tool) 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