• 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 Chimera3D · Jun 27, 2012 at 10:37 PM · physicsrigidbodyraycastforcehovercraft

Hovercraft Physics Problems

I've written my own hovercraft physics, however I can't seem to get it to work. I have Ray Position game objects that I place on different corners of the vehicle to dictate where the raycasts will go. These positions are then used to create the raycasts (going down), then determine where to add force on each corner of the object. I know I have nothing rotating or moving it forward, but what am I missing to get it off of the ground?

EDIT: Okay I figured out how to get it off of the ground with help from Wolfram, now it rocks and flips over a lot how can I fix that?

 #pragma strict
 
 var vehicleSpeed = 20;
 var rotationSpeed = 10.0;
 var airborneForce = 5.0;
 var offgroundHeight = 10.0;
 var startRotation : Quaternion;
 var startPosition : Vector3;
 var rayPos : GameObject[];
 
 function Start () {
     
     rayPos = GameObject.FindGameObjectsWithTag("RayPos");
     startRotation = transform.rotation;
     startPosition = transform.position;
     
 }
 
 function Update () {
     
     var down = transform.TransformDirection (-Vector3.up);
     
     var forwardForce : float = Input.GetAxis ("Vertical") * vehicleSpeed;
     
     var    flight = transform.TransformDirection (Vector3.up * airborneForce);
     
     rigidbody.AddForce (transform.forward * forwardForce);
     
     if(Input.GetAxis ("Horizontal")){
         
         var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
         transform.Rotate (0, rotation, 0);
         
     }
     
     if(Input.GetButtonDown("Jump")){
         rigidbody.drag = 50;
     }else
         
     if(Input.GetKeyDown("left ctrl")){
         transform.rotation = startRotation;
         transform.position.y += 2;
         rigidbody.drag = 50;
     }else
     
     if(Input.GetKeyDown("r")){
         transform.rotation = startRotation;
         transform.position = startPosition;
         transform.position.y += 2;
         rigidbody.drag = 50;
     }else
         
         rigidbody.drag = 0;
     
      if (Physics.Raycast (rayPos[0].transform.position, down, offgroundHeight)) {
              rigidbody.AddForceAtPosition(flight, rayPos[0].transform.position);
      }
      
      if (Physics.Raycast (rayPos[1].transform.position, down, offgroundHeight)) {
              rigidbody.AddForceAtPosition(flight, rayPos[1].transform.position);
      }
      
      if (Physics.Raycast (rayPos[2].transform.position, down, offgroundHeight)) {
              rigidbody.AddForceAtPosition(flight, rayPos[2].transform.position);
      }
      
      if (Physics.Raycast (rayPos[3].transform.position, down, offgroundHeight)) {
              rigidbody.AddForceAtPosition(flight, rayPos[3].transform.position);
      }
 
 }
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
1
Best Answer

Answer by Wolfram · Jun 27, 2012 at 10:53 PM

a) you are applying a downwards force to the rigidbody, but you need to apply an upwards force. Don't try to apply the "exhaust" from your hovercraft fans, apply the direction in which they would push your hovercraft: up.

b) note arrays start their count at 0, so if you have 4 objects tagged "RayPos", you'll need to access them using indices [0]..[3], not [1]..[4].

c) don't use any Find*-functions in Update, they are extremely slow. Assuming the amount/identity of objects tagged "RayPos" doen't change, do the FindGameObjectsWithTag() only once (i.e., in Start()). If the objects somehow do change, it might be a good idea to put the FindGameObjectsWithTag() in a separate function which is only called when they change.

EDIT: the script evolved while trying to solve all related problems, so the answer itself is rather outdated, but still contains hints to keep in mind. the rest of the answer is distributed in the comments ;-)

Comment
Add comment · Show 12 · 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 Chimera3D · Jun 27, 2012 at 11:07 PM 0
Share

I posted my updated script above, still not working...

avatar image Wolfram · Jun 27, 2012 at 11:39 PM 0
Share

Uh, wait, you are using "pos.rayPos[x]", which doesn't make any sense. pos is a Vector3, rayPos[x] is a GameObject. Does this even compile? Please add "#pragma strict" at the top of your script, to prevent mistakes like this.

Replace all occurences with this ins$$anonymous$$d: rayPos[x].transform.position

avatar image Chimera3D · Jun 27, 2012 at 11:44 PM 0
Share

Still not working.

avatar image Wolfram · Jun 27, 2012 at 11:51 PM 0
Share

Could you please update your question with your current script? Also, what exactly isn't working? Forward and rotation work? Doesn't it hover at all, or does it move/jump/jerk around?

avatar image Chimera3D · Jun 27, 2012 at 11:54 PM 0
Share

It doesn't hover at all. I'll post the current script.

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting a cannonball. 5 Answers

Direction of rotation and ApplyForce 1 Answer

airplane collision detection problem 1 Answer

How to access component that the raycast hit 1 Answer

Need a more detailed Physics documentation 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges