|
I have a sci-fi themed tank that I need to make it look like it's hovering up and down through C# script. How do I go about applying force to the rigidbody giving it the impression that it's hovering up and letting gravity bring it back down again all without increasing or editing the collider box? Where do I start? EDIT So I was playing around with the code I gave in the link below to see if it was an all in one solution and I think my freak of nature gravity value (-500) is affecting the code badly and my craft still sticks to the ground does anyone have a clue? Converted Code: using System.Collections; public class ScifiHover : MonoBehaviour { float forwardPower; }
(comments are locked)
|
|
Here's a part of my Hoverboard script. What it does is check the distance down to the ground; if it is less than the specified height, it applies upward force at a percentage of the force needed to lift it up. The - Time.deltaTime is actually just a small number that is used to make sure the hoverForceMultiplier doesn't go above 1. I have a Rigidbody of Mass 1, Use Gravity is Enabled (ticked). EDIT - here's the C# version : Original Post : It's in JS and right now I have no time to convert and check it. I shall make a C# vesion when I get home tonight =] example : http://www.alucardj.net16.net/unityquestions/HoverBoard6.html Wow ok thanks a million! I'll run it through a converter and work out the bugs and get back to you. EDIT Initial conversion successful however the vehicle just kept increasing it's hover height. I will continue to tweak and get back. Also I have to turn off gravity?
May 16 '12 at 03:46 AM
djdrool91
I just converted and tested this , and it seems to be working ok. I have a Rigidbody of Mass 1, Use Gravity is Enabled (ticked). I also updated the answer , let me know if you're still having problems with the gravity.
May 16 '12 at 07:39 AM
alucardj
You probably don't want to use physics, because transform.Translate(...) do not work well with non-kinematic rigid bodies and I really doubt that you want to move all of your units with physics (rigidbody.AddForce)
May 16 '12 at 12:46 PM
Tseng
I agree physics is probably not the way to go , but with no script in the question , and the asker stated "How do I go about applying force to the rigidbody", I had this script already made and it wasn't hard to convert, so I thought I would post it and maybe there would be some other ideas that could come out of it.
May 16 '12 at 01:32 PM
alucardj
Thanks for all the replies, I really appreaciate it. My apologies for not thoroughly giving more for you to help, I've just been working on it for awhile and have not familiarised myself with everything inside the project. Ok, so after testing Jay Kay's converted C# script, My tank does NOT hover when it starts with gravity on and mass of 1. I had to increase the force to a ridiculous number and it will fly into the air upon colliding with a static object or going up the ramp. Testing it with alucardj's conversion also results in the same situation. Both code makes the tank fall slower but it still sticks to the ground. Should I make it check for tags and tag my floors? Increasing the hover distance alone pulls the tank up forever only when going up a ramp. Sorry to be vague with my question but essentially I have controls and camera's all set however all I need left to do is to have the model bob up and down constantly even through movement and up ramps but without adding animation in Maya or something. I was suggested to add force for rigidbodies and since posted the question but what I want to know really is the best and simplest way to achieve this effect. Is there a way to not do it through physics or rather a "Hard-coded" method to achieving this? Edit Ok so I might have found the problem or part of it. When I print out the currentHeight variable, the results I get is weird. My tank starts at Y position 138 and the highest point in the entire map is 180 however according to the print I get 138 for my lowest point (correct) but when I am going up ramps or even on any slight descending or ascending planes, it gives me a single digit print. Ok I'm still having the gravity problem however in one of the test's I noticed that it hovered (I had to add like 3000 force) but highly unbalanced. How do I prevent this? I saw this guy's video (http://virtualrealitydesign4.blogspot.com/2012/01/script-for-hovercraft-mechanics.html) where he used spheres at the corners for balancing. MAJOR EDIT I've just been told that the project's gravity value was editted to -500. So do i change the value of 9.8 inside the code to 500 as I'm guessing that's the gravity value by default. Ok so if I changed that value to 500, It works! With Hover Distance at 10 it will float and bob up and down! However the movements like how II feared. Highly unstable when going up ramps. Is there any work around to this problem?
May 17 '12 at 02:58 AM
djdrool91
(comments are locked)
|
|
As asafsitner mentioned, it's best to do it via animation, as it allows you to add unsteady hovering which also depends on the current state of the tank (i.e. moving, shooting, standing, searching). You can always use Mathf.Cos or Mathf.Sin, as they will produce a periodical value which ranges from -1 to 1. I would love to do it through animation however the current circumstance is not allowing me to do so and thus has to be done by script. I'm not too good with C# and unity so if you could help me get started on how to use Mathf.Cos or Mathf.Sin I would really appreaciate it. What I'm trying to achieve is something like on this video (http://www.youtube.com/watch?v=JE0ZLq5bobc) however my controller and camera are all set I just simply need to make the tank hover up and down by time intervals.
May 16 '12 at 02:46 AM
djdrool91
(comments are locked)
|

I believe animation would work better and provide more predictable results in this case.