How to make a cube maintain distance?

I’m having trouble scripting in c# to make a cube with a RigidBody using gravity with a downwards raycast to maintain a distance from other game objects ( terrain, other cubes ect) i don’t want the cube to have a hover effect or spring like effect i just need to script to stop its downwards movement if it comes any closer than than 1 meter or the recast length. if anyone could help that would be great :slight_smile:

this is a image of what I’m trying to achieve :slight_smile:

as i mentioned you can archive this with colliders and 0 lines of code. but people giving weird answers here for a raycast solution.

Raycast Solution

theory: We send a raycast down, saving the point we hit, sending a raycast from this point up hitting our cube. get the distance of the second ray substract it from 1m and add it to your cube position if its positive.

you should avoid forces if you dont want a hovering effect

[60011-maintainheightdemo.zip|60011]

It’s simpler if slope and plane both are different objects. I really wouldn’t use raycasting in this case.
What I’d use is make a float variable called ‘distance’. Then equal it to Vector2.Distance();
like :
distance = Vector2.Distance(); in parameters , I’d put cube’s position as A point and plane’s position as B point, Then I’ll check if distance is less than 1 meter then distance is 1 meter.

Or another way may be saying transform.position.y = target.position.y + 10;

I haven’t really checked these 2 methods but you can try them :slight_smile:

Another way to do this would be creating an raycast which can discern an distance of x-units, and going by that value I would just use addForce to keep the object midair, on another note this would result in an kind-of hoverboard effect.

Try something like this:

    public Rigidbody2D body;
    public float hoverDist;
    void Hover()
    {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 100, 1<<LayerMask.NameToLayer("GroundLayer"));
        if (hit.distance < hoverDist)
        {
            body.AddForce(Vector2.up);//You will need to make this force relative to your gravity scale, object mass, etc...
        }
    }

You basically raycast down a certain distance, save the RaycastHit2D info, if it hits your layer and it’s less than X distance from that hit/layer, it will add force. Be sure to adjust the force by multiplying the Vector2.up static by your gravity value (prob 9.81, but could be different). Basically you want enough force to overcome gravity, but not much more. Since it will be “turned off” at a certain height, this may create a bouncing effect however. Depending on if this is physics based game or not, this may not matter…

Hope this helps. Cheers.

Sorry to dig up this old thread but having a similar issue and the above solution from @Soraphis is the closest I’ve gotten to a full resolution.

I’m building a platformer test, and I want the character to be able to jump underneath and on top of a cube that reacts to the characters velocity but settles back into position.

The above solution works well for jumping and hitting the underside of the cube, but when the character jumps on top it freaks out until the character falls through the object.

Any idea how to build this without relying on raycasting downwards?