• 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
Question by djdrool91 · May 21, 2012 at 07:02 AM · tutorialvehicleshovercraft

Hovercraft Non Working (Stability)

So I posted this question before but I have a different one now. I want to make a hovercraft in game and I found this guy's script here: (http://virtualrealitydesign4.blogspot.com/2012/01/script-for-hovercraft-mechanics.html)

which has 2 video parts here: (http://www.youtube.com/watch?feature=player_embedded&v=IYpwvtc0Fk8)

and here (http://www.youtube.com/watch?feature=player_embedded&v=ssZ0BE7s9zA).

I want to achieve that effect exactly however I can't seem to get his script to work in C#. I used a converter here: (http://files.m2h.nl//js_to_c.php) or even in Javascript.

Can anyone help me with this like how to set up my models and parenting and explaining how this works because I'm not too good at this and the creator seems to not be active anymore.

@hijinxbassist I appreciate the answer and yes I did that and got it to work however not perfectly. Because I only worked with Javascript and C# since march, I'm not good at it.

@syclamoth Thanks a million! Here is the code I converted and got it to work however with a flaw. The 5 Spheres that takes care of stability are not at the four corners but in the center stuck together which messes up my collision stuff.

Here is the converted code:

 using UnityEngine;
 using System.Collections;
 
 public class ScifiHover : MonoBehaviour
 {
     public float forwardPower = 75000.0f;
     public float steerPower = 100000.0f;
     public float landingPower = 15.0f;
     public float jumpingPower = 15.0f;
     public float hoverHeight = 15.0f;
     public float stability = 2;
     public GameObject body;
     public float speedUpdate;
     private Vector3[] hitNormal = new Vector3[5];
     private Quaternion rotation;
     private float increment;
     private Vector3[] lastNormals = new Vector3[5];
     private bool physicsSetup = false;
     private Vector3 boxDim;
     private Vector3[] cornersPoint = new Vector3[5];
     private Transform[] corners = new Transform[5];
     private BoxCollider boxCollider;
     private float yBounce;
     private Vector3 lastPosition;
     private float distance;
     private Vector3 average;
 
     void Awake()
     {
         InitializePhysics();
     }
 
     void Update()
     {
         CalculateSpeed();
     }
 
     void FixedUpdate()
     {
         if (physicsSetup)
         {
             RaycastHit hit;
 
             for (int i = 0; i <= corners.Length - 1; i++)
             {
                 if (Physics.Raycast(corners[i].position, -corners[i].up, out hit, (hoverHeight + 5)))
                 {
                     if (hit.collider.gameObject.tag == "Floor")
                     {
                         hitNormal[i] = body.transform.InverseTransformDirection(hit.normal);
                         if (lastNormals[i] != hitNormal[i])
                         {
                             increment = 0;
                             lastNormals[i] = hitNormal[i];
                         }
                         distance = hit.distance;
                         if (hit.distance < hoverHeight)
                         {
                             constantForce.relativeForce = (-average + transform.up) * rigidbody.mass * jumpingPower * rigidbody.drag * Mathf.Min(hoverHeight, hoverHeight / distance);
                         }
                         else
                         {
                             constantForce.relativeForce = -(transform.up) * rigidbody.mass * landingPower * rigidbody.drag / Mathf.Min(hoverHeight, hoverHeight / distance);
                         }
                     }
                 }
             }
             average = -(hitNormal[0] + hitNormal[1] + hitNormal[2] + hitNormal[3] + hitNormal[4]) / 2;
 
             if (increment != 1) { increment += 0.03f; }
 
             rotation = Quaternion.Slerp(body.transform.localRotation, Quaternion.Euler(average * Mathf.Rad2Deg), increment);
             Quaternion temp = rotation;
 
             temp.y = transform.up.y * Mathf.Deg2Rad;
             body.transform.localRotation = temp;
 
             float fwdForce = Input.GetAxis("Vertical") * forwardPower;
             rigidbody.AddForce(transform.forward * fwdForce);
 
             float steerForce = Input.GetAxis("Horizontal") * steerPower;
             rigidbody.AddTorque(transform.up * steerForce);
         }
     }
 
     void OnDrawGizmos()
     {
         if (corners[0] != null) { Gizmos.DrawWireSphere(corners[0].position, 5); }
         if (corners[1] != null) { Gizmos.DrawWireSphere(corners[1].position, 5); }
         if (corners[2] != null) { Gizmos.DrawWireSphere(corners[2].position, 5); }
         if (corners[3] != null) { Gizmos.DrawWireSphere(corners[3].position, 5); }
         if (corners[4] != null) { Gizmos.DrawWireSphere(corners[4].position, 5); }
     }
 
     void CalculateSpeed()
     {
         if (lastPosition != transform.position)
         {
             float distance = Vector3.Distance(transform.position, lastPosition);
             speedUpdate = (distance / 1000) / (Time.deltaTime / 3600); //Km/h
         }
     }
 
     void InitializePhysics()
     {
         //This function will add a temporary box collider and get the corner positions
         boxCollider = body.AddComponent<BoxCollider>();
         boxDim = new Vector3(boxCollider.size.x * body.transform.localScale.x, boxCollider.size.y * body.transform.localScale.y, boxCollider.size.z * body.transform.localScale.z * stability);
         cornersPoint[0] = new Vector3(transform.position.x - boxDim.x / 2, transform.position.y - boxDim.y / 2, transform.position.z + boxDim.z / 2);
 
         cornersPoint[1] = new Vector3(boxDim.x / 2 + transform.position.x, transform.position.y - boxDim.y / 2, transform.position.z + boxDim.z / 2);
         cornersPoint[2] = new Vector3(boxDim.x / 2 + transform.position.x, transform.position.y - boxDim.y / 2, transform.position.z - boxDim.z / 2);
         cornersPoint[3] = new Vector3(transform.position.x - boxDim.x / 2, transform.position.y - boxDim.y / 2, transform.position.z - boxDim.z / 2);
         cornersPoint[4] = transform.position;
         Destroy(boxCollider);
 
         for (int i = 0; i <= cornersPoint.Length - 1; i++)
         {
             GameObject stablePlatform = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             stablePlatform.name = "StablePlatform" + "(" + i + ")";
             stablePlatform.transform.parent = body.transform;
             stablePlatform.transform.localPosition = transform.InverseTransformPoint(cornersPoint[i]);
             corners[i] = stablePlatform.transform;
             Destroy(stablePlatform.GetComponent<MeshRenderer>());
             Destroy(stablePlatform.GetComponent<Collider>());
         }
         cornersPoint = null;
         physicsSetup = true;
     }
 }
Comment

People who like this

0 Show 8
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 hijinxbassist · May 21, 2012 at 09:51 AM 0
Share

You should just leave it as a JS file, converters arent always 100% accurate and if you cant read the language you wont know if it screwed up something(unless the console throws an error). The script should do all the work for you..so my advice is to leave it as a JS and watch the videos as many times as it takes. The originator of the script was nice enough to post 2 videos explaining how this works and how to set it up... If something in particular isnt working as expected, you can update you "question" to reflect as such.

avatar image syclamoth · May 21, 2012 at 10:02 AM 0
Share

I do Javascript to C# conversions! A human can do the job much better than a machine can. Just post the converted C# code, and I'll see if I can troubleshoot it for you.

avatar image syclamoth · May 21, 2012 at 10:18 AM 0
Share

Urggh it's long. Give me a minute.

avatar image Bunny83 · May 21, 2012 at 10:42 AM 3
Share

Seriously, the video is more like a tutorial. It shows everything. The hierarchy and how the script works. Well he sounds a bit unconfident and some of the code could be improved, but he managed it get get it working and showed everything.

So basically this is a whole tutorial and you want us to work through it and present you the result?

avatar image Bunny83 · May 22, 2012 at 11:57 AM 1
Share

Ok, i've just created a test scene in my test project and put this script in there. I used a way smaller scale (my box scale is [1, 0.2, 2] instead of [20, 1, 40]) so i had to scale all variables down. It does work after i setup everything the way it's in the video:

  • created empty hover object, attached the script, a rigidbody and a constantforce component

  • Set the rigidbody drag / angular drag and mass values

  • tagged the terrain "Floor"

  • created a cube object scaled it the way i need it and attached it at child to the hover

  • dragged the cube object (body) to the body variable of the script

  • removed the BoxCollider from the cubeobject (since he doesn't need collisions and it would make the temp BoxCollider fail).

  • adjusted all the script parameters to match my scale

Final result: It works, but just ok. In extreme situations the hover does really strange things and this has several reasons:

  • The code is overcomplicated at many spots. There are many veriables that are never used or they have no effect

  • The whole constant force setup doesn't make much sense. In the for loop only the distance of the last corner (the center) is taken into account.

  • way too much magic numbers and equatations that make no sense.

  • The whole rotation of the body doesn't make any sense. I'm supprised that is actually works the way he's set it up. He used a direction vector as eulerangles and finally manipulated the quaternions y component manually and assigned another direction vector component. This all doesn't make any sense.

  • The speed calculation (which seems to be calculated just for display or something) is wrong because lastPosition is never set to anything. Even Visual Studio is throwing a warning.

  • Finally some things that aren't necessary. The stablePlatform GOs aren't needed. They are aligned the same way as the body so the direction for the raycast is the same for each "corner". All you need is the position of the corners. Those stablePlatform GOs should actually be empty gameobjects since he destroyed all components after he created it. So a "`new GameObject`" would be enough.

To conclude: It's nice that people share their code and it's nice that people like you want to translate it for others, but this script is just, well, crap ;) It's better and propably easier to write it from scratch.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Metaball problem? 1 Answer

FPSPlayer script from FPS tutorial won't work... 0 Answers

Buttons in input manager don't do anything 1 Answer

Issues with C# script (using tutorial) 1 Answer

Error CS1502 help! 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