How can I make my time bombs hover a set distance from the ground? And I would prefer if it was smoothed so it wont jump to the ground but just float down. Has anybody already done this? :?
If you want to make something hover up and down when it nears a solid surface, use this code:
function FixedUpdate () {
if (Physics.Raycast (transform.position, Vector3.down, 1.5)) {
rigidbody.AddForce (Vector3.up * 7);
}
}
Obviously it has to have a rigidbody, and the 1.5 and 7 can be changed to suit your project.
It just falls. And it doesn’t say why.
In that case, increase the 7 until you get the desired effect.
There’s a script file attached to this topic that does a hovering effect. It’s actually for a hovering vehicle but you could easily take out the arrow key controls and other unnecessary stuff. It’s designed so that the vehicle can be used on a spherical planet so you will need to take out the spherical “gravity” force (look for the “Gravity toward the planet” comment in FixedUpdate) to use it with Unity’s built-in gravity.
It Worked!
andeeee…
YOU SCARE ME!!!
← Yeah, it’s not a very flattering photograph of me is it?
Do most of you have an Intel Mac?
How do I make the script have “var” in it?
“var” is a Pro-only feature.
Zing!
Ghost, a lot of people around here have told you that you need to start a bit simpler with your Unity Projects… I think your last question there sums it up for me. Just be a little more patient with learning how to make games (it’s not that easy) and start smaller.
Ethan
Ummm… No. How is it a pro only feature?
Ghost please do yourself a favour and read through this:
http://www.unifycommunity.com/wiki/index.php?title=Programming
So to make the above script a bit more robust and less hardcoded do this:
var rayLength : float = 1.5;
var hoverForce : float = 7.0;
function FixedUpdate () {
if (Physics.Raycast (transform.position, Vector3.down, rayLength)) {
rigidbody.AddForce (Vector3.up * hoverForce);
}
}
Cheers.
I hadn’t seen this guide before thylaxene just pointed it out. Having looked through it, I recommend it as reading for anyone who is using Unity and hasn’t done any serious object-oriented programming before (this of course includes people with no programming experience at all).
This is great - thanks.
However … how do i get to this page from the Wiki
index ? ( i know this must sound dumb - but i
can’t find the link ) - it shows up using the search
function but … where is that link ?
cheers
louis
in the left navigation bar click ‘tutorials’ - its the top one on that page ; )
of course !
cheers
louis