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 sjj100 · Oct 12, 2012 at 03:09 AM · terrainobjectfloatingon

Keep an object on the gound.

Hello, I am using this script and the object I apply this script to floats in the air as it attempts to follow my character. How can I get the object to remain on the terrain as it follows my character? (the character changes heights on the terrain)

Thank you.

 var target : Transform; //the enemy's target
 var moveSpeed = 3; //move speed
 var rotationSpeed = 3; //speed of turning
 
 var myTransform : Transform; //current transform data of this enemy
 
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
 
 function Start()
 {
      target = GameObject.FindWithTag("Player").transform; //target the player
 
 }
 
 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 
 }
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

Answer by Noah-1 · Oct 12, 2012 at 03:21 AM

I had the exact same problem sometime ago, here is the solution:

The object is floating in the air because it is following the center pivot of your character (which is probably higher than your follower), if you want it to stay on the ground, just create an empty gameObject put it at the height of your character follower, but close to your character and attach it to him, add a tag to it (like targetPoint) and instead of looking for the player, look for that point:

 target = GameObject.FindWithTag("targetPoint").transform; //target the player
 

Hope it Helps :)

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 sjj100 · Oct 12, 2012 at 03:44 AM 0
Share

This would work if my player was moving at a constant height, but it is varying. Thank you for the response.

avatar image
0

Answer by NowhereStudios · Oct 12, 2012 at 03:23 AM

Easiest way is, you can make a raycast from your object to Vector3.down so you can find the point to snap on each frame.

Edit: I have not test it but possibly similar solution like this: Edit2 : I have changed the code. i rewrite it for java script. it would still give some possible errors, notify me further ;)

 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

     //EDIT DONE HERE
     RaycastHit myHit;
     var offsetAlign:float = 0.0; //Add offset to make your object align perfect to terrain
     if(Physics.Raycast (myTransform.position, Vector3.down, myHit))
     {
          myTransform.position.y = myHit.point + offsetAlign;
     }

     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 }
Comment
Add comment · Show 5 · 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 sjj100 · Oct 12, 2012 at 03:55 AM 0
Share

I seem to be encountering some compile errors with NowhereStudio's answer... (24,15) ';' expected. Insert semicolon at the end. (25,10) ';' expected. Insert semicolon at the end.

avatar image NowhereStudios · Oct 13, 2012 at 09:53 PM 0
Share

try to add only "edit done here" written block to your previous not error giving update function

avatar image NowhereStudios · Oct 13, 2012 at 09:54 PM 0
Share

aha i found i guess you are not using csharp i edited the post above

avatar image ODxT · Jun 09, 2015 at 05:41 PM 0
Share

I get an error saying that it expects to be a ; in between RaycastHit and myHit.

      RaycastHit myHit;

                ^
              Here

 
avatar image MTDues · Feb 06, 2017 at 04:23 PM -1
Share

not working :((((

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't use tree brush tool 0 Answers

Turret floats away and pushes everything away from it 2 Answers

Object floating 7 Answers

Make a simple tree 1 Answer

Tree position on random Terrain (y axis) 0 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