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 forferdeilig · Sep 17, 2013 at 11:44 PM · javascriptphysicshovering

Hovering script, set deceleration

Hi, i'm trying to make a vehicle hover a certain height above the ground, I've either made or found this script ( i haven't worked on this for a while), which does sort of work but is not ideal as it just bounces around far too much

What i'm really trying to do is make a script which detects how far you are from the ground, and sets a deceleration for that, however i've only just started to learn js so how would you do this? How could you make the force added a function of the change in distance between the vehicle and ground, up to a limited maximum value?

SO as it gets closer to its target distance, the amount of force applied is less so it doesn't go over its target distance.

(current script)

 #pragma strict
 
 function Start () {
 
 }
 var hoverHeight : float = 10;
 
 function Update () 
 {
 
 var ray = this.transform.position;
  
 if (Physics.Raycast(transform.position, -transform.up, hoverHeight))
 
 {
         rigidbody.AddForce(0,100,0);
         
 }
 }
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 meat5000 · Sep 17, 2013 at 11:45 PM

Simply make your AddForce call contain a function of distance and a scaling factor.

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 forferdeilig · Sep 18, 2013 at 11:29 AM 0
Share

Hi, thanks for the reply. How can you get The distance, i changed the script a bit after a lot of googling but i really don't know anything about coding, all i've done was a tiny bit of vb a very long time ago. New script

 #pragma strict
 
 function Start () {
 
 }
 var hoverHeight : float = 5;
 
 function Update () 
 {
 var groundLoc = GameObject.Find("ground").transform.position;
 var distance = Vector3.Distance(this.transform.position, groundLoc.transform.position);
  
 if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;  
 
 {
     
 }
 }

the problems i need to fix with this are,

  1. on line 11 I get "bce0019: 'transform' is not a member of 'UnityEngine.vector3'.

  2. I'm not sure that will give me the distance i want, as i only want distance in the global y axis.

  3. How is it possible to do the following: if(distance < hoverHeight, set distance to = hoverHeight at a certain rate;

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Add Explosion Force without torque? 1 Answer

Copy rotation values in between objects. 1 Answer

Physics driven cars without Wheel Colliders. Is it practicable? 1 Answer

Rigidbody hover, bodies connected with joints should hover together. 0 Answers

JavaScript 3 Arrays questions 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