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 bagelbite · Dec 02, 2014 at 01:42 AM · raycastvector3hit.point

Moving gameobject along terrain based on player movement using raycast

I want to be able to have objects that are always on the ground, but move based on the player position. Because of hills in the terrain the object needs to be able to raise and lower in the Y axis as the ground goes up and down. The solution I came up with is to have the parent object, that is a child of the player, raycast to the ground and then adjust the child object's y position based on that. However I can not for the life of me figure out how to get it to work.

This is the code I tried. Any insight would be much appreciated.

Thanks :)

selectedItem is the object I want to appear on the ground.

placeSpot is the parent object that is a child of the player.

In the place of hit.normal.y I have also tried tempY3, hit.point.y, hit.transform.localPosition.y, and hit.transform.position.y

 void Update () 
     {
 
         Debug.DrawRay(placeSpot.transform.position, -Vector3.up, Color.green);
 
 
         if(placingItem)
         {
             RaycastHit hit;
             Physics.Raycast(placeSpot.transform.position, Vector3.down, out hit);
             if(Physics.Raycast(placeSpot.transform.position, Vector3.down, 100))
             {
                 Debug.Log(hit.distance);
                 tempY = hit.distance;
                 float tempY2 = selectedItem.transform.localPosition.y;
                 float tempY3 = tempY - tempY2;
                 selectedItem.transform.localPosition = new Vector3(placeSpot.transform.localPosition.x, hit.normal.y, placeSpot.transform.localPosition.z);
             }
             selectedItem.transform.rotation = Quaternion.identity;
             selectedItem.transform.parent = placeSpot.transform;
             selectedItem.transform.localRotation = placeSpot.transform.localRotation;
         }
     }
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
0

Answer by troien · Dec 02, 2014 at 09:27 AM

Well, first of all, there is no need to raycast twice. Physics.Raycast returns true when it hits something. Currently you are using the first raycast it's hit when the second raycast hits something, that doesn't really make sense.

Also, read the docs of RaycastHit. hit.point is probably th Vector3 you are looking for.

I added a piece of code that should place the 'selectedItem' at the first thing it hits when casting a ray down. If it places it at the exact same position as placeSpot. Then placeSpot probably has a collider attatched to it that you hit. In that case you'll need to ignore the placeSpot's collider (Example is for linecast, but the layerMask parameter works in the same way)

         float distance = 100; // I use this value to make sure the Debug ray is identical (Of the same length) to the Ray I cast.
         Debug.DrawRay(placeSpot.transform.position, Vector3.down * distance, Color.green);
 
         if (placingItem)
         {
             RaycastHit hit;
             if (Physics.Raycast(placeSpot.transform.position, Vector3.down, distance, out hit))
             {
                 Debug.Log(hit.point);
                 selectedItem.transform.parent = placeSpot.transform;
                 selectedItem.transform.localRotation = placeSpot.transform.localRotation;
                 selectedItem.transform.position = hit.point;
             }
         }

If this does not help for your problem, then please describe your problem better. Because a.t.m. you are telling us what your code is, you are telling us what it should do, but you aren't telling us what it actually does (is the Debug.Log logged, where is your selectedItem placed? etc.)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AddForce to the Left? 3 Answers

My Raycast on awake fires and will not change Help! 0 Answers

Orientate a quad to the ray cast hit.normal 1 Answer

[Solved]Terrain: Rotatating an object to the hit.normal + manual rotation 1 Answer

GameObject facing hit.point doesn't always work 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