Basket physics under a hot air balloon

Hey guys,

I just started looking at a joint setup for my hot air balloon character. For simplicity, imagine a sphere and a little cylinder (basket) underneath it.

The game is set so that the balloon character (a rigid body) has a constant velocity upwards and the player steers the balloon by giving it velocity to either the left or the right.

Because of the nature of the game, the rigidbody settings for the balloon are set to NOT use gravity. After all, I need it to just go upwards at a constant velocity. This seems to be where the problem lies. If I now attach a Hinge Joint to the basket under it, I want it to swing and come to a rest as if it IS effected by gravity. However, when I enable gravity on the basket, the whole balloon starts to fall (it seems to inherit the gravity setting).

Is there a way around this?

It isnt inheriting the gravity settings. When a balloon is flying in the air, and then you suddenly attatch a sandbag to it, it is most likely going to start falling if the balloon cannot handle the weight.

To fight this, you can maybe increase the velocity power of your hot air balloon so it can handle the basket. By increasing its power, you can even enable gravity on it.

Thanks for the reply. I did as you suggested and it works great. One more question…

Do you have any recommendations for the joint setup for this balloon (see image below). The balloon has a basket, and ropes that attach the balloon to the basket. I can make the basket sway underneath, but I’d like the ropes to tick to it as well.

So you want physics like ropes?
You might need to look into SkinnedMeshRenderer Unity - Manual: Skinned Mesh Renderer component
Here is a video with an example
Click for video

There was a script on the wiki about ropes. Maybe you can play with that.
I didnt really look at the code, but it seems like a giant mess and you are most likely better off doing something like what the guy in the video did.
Click for Rope scripts

This script you place on a gameobject

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
public class Rope : MonoBehaviour
{
    //============================
    //==    Physics Based 3D Rope ==
    //==    File: Rope_Tube.js    ==
    //==    By: Jacob Fletcher    ==
    //==    Use and alter Freely    ==
    //============================
    //To see other things I have created, visit me at www.reverieinterative.com
    //How To Use:
    // ( BASIC )
    // 1. Simply add this script to the object you want a rope teathered to
    // 3. Assign the other end of the rope as the "Target" object in this script
    // 4. Play and enjoy!
    // (About Character Joints)
    // Sometimes your rope needs to be very limp and by that I mean NO SPRINGY EFFECT.
    // In order to do this, you must loosen it up using the swingAxis and twist limits.
    // For example, On my joints in my drawing app, I set the swingAxis to (0,0,1) sense
    // the only axis I want to swing is the Z axis (facing the camera) and the other settings to around -100 or 100.

    public Transform target;
    public Material material;
    public float ropeWidth = 0.5f;
    public float resolution = 0.5f;
    public float ropeDrag = 0.1f;
    public float ropeMass = 0.5f;
    public int radialSegments = 6;
    public bool startRestrained = true;
    public bool endRestrained = false;
    public bool useMeshCollision = false;

    Vector3[] segmentPos;
    GameObject[] joints;
    GameObject tubeRenderer;
    TubeRenderer line;
    int segments = 4;
    bool rope = false;

    //Joint Settings
    Vector3 swingAxis = new Vector3(0, 1, 0);
    float lowTwistLimit = 0.0f;
    float highTwistLimit = 0.0f;
    float swing1Limit = 20.0f;
 
    void OnDrawGizmos()
    {
        if(target) {
            Gizmos.color = Color.yellow;
            Gizmos.DrawLine (transform.position, target.position);
            Gizmos.DrawWireSphere ((transform.position+target.position)/2,ropeWidth);
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere (transform.position, ropeWidth);
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere (target.position, ropeWidth);
        } else {
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere (transform.position, ropeWidth); 
        }
    }
 
    void Awake()
    {
        if(target) {
            BuildRope();
        } else {
            Debug.LogError("You must have a gameobject attached to target: " + this.name,this); 
        }
    }
 
    void LateUpdate()
    {
        if(target)
        {
            // Does rope exist? If so, update its position
            if(rope)
            {
                line.SetPoints(segmentPos, ropeWidth, Color.white);
                line.enabled = true;
                segmentPos[0] = transform.position;
                for(int s=1;s<segments;s++)
                {
                    segmentPos[s] = joints[s].transform.position;
                }
            }
        }
    }
 
    void BuildRope()
    {
        tubeRenderer = new GameObject("TubeRenderer_" + gameObject.name);
        line = tubeRenderer.AddComponent<TubeRenderer>();
        line.useMeshCollision = useMeshCollision;
        // Find the amount of segments based on the distance and resolution
        // Example: [resolution of 1.0f = 1 joint per unit of distance]
        segments = (int)(Vector3.Distance(transform.position,target.position)*resolution);
        if(material != null)
        {
            material.SetTextureScale("_MainTex", new Vector2(1,segments+2));
            if(material.GetTexture("_BumpMap"))
                material.SetTextureScale("_BumpMap", new Vector2(1,segments+2));
        }
        line.vertices = new TubeVertex[segments];
        line.crossSegments = radialSegments;
        line.material = material;
        line.SetMaterial();
        segmentPos = new Vector3[segments];
        joints = new GameObject[segments];
        segmentPos[0] = transform.position;
        segmentPos[segments-1] = target.position;
        // Find the distance between each segment
        int segs= segments-1;
        Vector3 seperation = ((target.position - transform.position) / segs);
        for(int s=0;s < segments;s++)
        {
            // Find the each segments position using the slope from above
            Vector3 vector = (seperation*s) + transform.position; 
            segmentPos[s] = vector;
            //Add Physics to the segments
            AddJointPhysics(s);
        }
        // Attach the joints to the target object and parent it to this object
        CharacterJoint end = target.gameObject.AddComponent<CharacterJoint>();
        end.connectedBody = joints[joints.Length-1].transform.GetComponent<Rigidbody>();
        end.swingAxis = swingAxis;

        SoftJointLimit lowTwistSoftJointLimit = new SoftJointLimit();
        lowTwistSoftJointLimit.limit = lowTwistLimit;
        end.lowTwistLimit = lowTwistSoftJointLimit;

        SoftJointLimit highTwistSoftJointLimit = new SoftJointLimit();
        highTwistSoftJointLimit.limit = highTwistLimit;
        end.highTwistLimit = highTwistSoftJointLimit;

        SoftJointLimit swing1SoftJointLimit = new SoftJointLimit();
        swing1SoftJointLimit.limit = swing1Limit;
        end.swing1Limit = swing1SoftJointLimit;

        target.parent = transform;
        if(endRestrained)
        {
            end.GetComponent<Rigidbody>().isKinematic = true;
        }
        if(startRestrained)
        {
            transform.GetComponent<Rigidbody>().isKinematic = true;
        }
        // Rope = true, The rope now exists in the scene!
        rope = true;
    }
 
    void AddJointPhysics(int n)
    {
        joints[n] = new GameObject("Joint_" + n);
        joints[n].transform.parent = transform;
        Rigidbody rigid = joints[n].AddComponent<Rigidbody>();
        if(!useMeshCollision) {
            SphereCollider col = joints[n].AddComponent<SphereCollider>();
            col.radius = ropeWidth;
        }
        CharacterJoint ph = joints[n].AddComponent<CharacterJoint>();
        ph.swingAxis = swingAxis;

        SoftJointLimit lowTwistSoftJointLimit = new SoftJointLimit();
        lowTwistSoftJointLimit.limit = lowTwistLimit;
        ph.lowTwistLimit = lowTwistSoftJointLimit;
     
        SoftJointLimit highTwistSoftJointLimit = new SoftJointLimit();
        highTwistSoftJointLimit.limit = highTwistLimit;
        ph.highTwistLimit = highTwistSoftJointLimit;
     
        SoftJointLimit swing1SoftJointLimit = new SoftJointLimit();
        swing1SoftJointLimit.limit = swing1Limit;
        ph.swing1Limit = swing1SoftJointLimit;

        //ph.breakForce = ropeBreakForce; <--------------- TODO
        joints[n].transform.position = segmentPos[n];
        rigid.drag = ropeDrag;
        rigid.mass = ropeMass;
        if(n==0){            ph.connectedBody = transform.GetComponent<Rigidbody>();
        } else
        {
            ph.connectedBody = joints[n-1].GetComponent<Rigidbody>(); 
        }
    }
}

These 2 scripts you dont place yourself

using UnityEngine;
using System.Collections;

public class TubeRenderer : MonoBehaviour
{
    public TubeVertex[] vertices;
    public Material material;
    public int crossSegments = 3;
    public float flatAtDistance = -1;
    public int movePixelsForRebuild = 6;
    public float maxRebuildTime = .1f;
    public bool useMeshCollision;
 
    Vector3 lastCameraPosition1;
    Vector3 lastCameraPosition2;
    Vector3[] crossPoints;
    int lastCrossSegments;
    float lastRebuildTime;
    Mesh mesh;
    bool colliderExists;
    bool usingBumpmap;
    Renderer myRenderer;
 
    void Reset()
    {
        vertices = new TubeVertex[] {new TubeVertex(Vector3.zero, 1.0f, Color.white), new TubeVertex(new Vector3(1,0,0), 1.0f, Color.white)};
    }
 
    void Awake()
    {
        Reset();
        mesh = new Mesh();
        gameObject.AddComponent<MeshFilter>();
        MeshRenderer mr = gameObject.AddComponent<MeshRenderer>();
        myRenderer = gameObject.GetComponent<Renderer>();
    }

    public void SetMaterial()
    {
        gameObject.GetComponent<MeshRenderer>().material = material;
        if(material)
        {
            if(material.GetTexture("_BumpMap")) usingBumpmap = true;
        }
    }

    void LateUpdate()
    {
        if(vertices == null || vertices.Length <= 1)
        {
            myRenderer.enabled = false;
            return;
        }

        myRenderer.enabled = true;
        if(crossSegments != lastCrossSegments)
        {
            crossPoints = new Vector3[crossSegments];
            float theta = 2.0f*Mathf.PI/crossSegments;
            for(int c=0;c<crossSegments;c++)
            {
                crossPoints[c] = new Vector3(Mathf.Cos(theta*c), Mathf.Sin(theta*c), 0);
            }
            lastCrossSegments = crossSegments;
        }
     
        Vector3[] meshVertices = new Vector3[vertices.Length*crossSegments];
        Vector2[] uvs = new Vector2[vertices.Length*crossSegments];
        Color[] colors = new Color[vertices.Length*crossSegments];
        int[] tris = new int[vertices.Length*crossSegments*6];
        int[] lastVertices = new int[crossSegments];
        int[] theseVertices = new int[crossSegments];
        Quaternion rotation = new Quaternion();
     
        for(int p=0;p<vertices.Length;p++)
        {
            if(p<vertices.Length-1)
                rotation = Quaternion.FromToRotation(Vector3.forward,vertices[p+1].point-vertices[p].point);
         
            for(int c=0;c<crossSegments;c++)
            {
                int vertexIndex = p*crossSegments+c;
                meshVertices[vertexIndex] = vertices[p].point + rotation * crossPoints[c] * vertices[p].radius;
                uvs[vertexIndex] = new Vector2((0.0f+c)/crossSegments,(0.0f+p)/vertices.Length);
                colors[vertexIndex] = vertices[p].color;
             
                lastVertices[c]=theseVertices[c];
                theseVertices[c] = p*crossSegments+c;
            }
         
            //make triangles
            if (p>0) {
                for (int c=0;c<crossSegments;c++) {
                    int start= (p*crossSegments+c)*6;
                    tris[start] = lastVertices[c];
                    tris[start+1] = lastVertices[(c+1)%crossSegments];
                    tris[start+2] = theseVertices[c];
                    tris[start+3] = tris[start+2];
                    tris[start+4] = tris[start+1];
                    tris[start+5] = theseVertices[(c+1)%crossSegments];
                }
            }
        }
     
        //Clear mesh for new build  (jf) 
        mesh.Clear();
        mesh.vertices = meshVertices;
        mesh.triangles = tris;
        mesh.RecalculateNormals();
        if(usingBumpmap)
            mesh.tangents = CalculateTangents(meshVertices);
        mesh.uv = uvs;
     
        if(useMeshCollision)
        if(colliderExists) {
            gameObject.GetComponent<MeshCollider>().sharedMesh = mesh;
        } else {
            gameObject.AddComponent<MeshCollider>();
            colliderExists = true;
        }
        GetComponent<MeshFilter>().mesh = mesh;
    }
 
 
 
    Vector4[] CalculateTangents(Vector3[] verts)
    {
        Vector4[] tangents = new Vector4[verts.Length];
     
        for(int i=0;i<tangents.Length;i++)
        {
            Vector3 vertex1 = i > 0 ? verts[i-1] : verts[i];
            Vector3 vertex2 = i < tangents.Length - 1 ? verts[i+1] : verts[i];
            Vector3 tan = (vertex1 - vertex2).normalized;
            tangents[i] = new Vector4( tan.x, tan.y, tan.z, 1.0f );
        }
        return tangents; 
    }
 
 
 
    //sets all the points to points of a Vector3 array, as well as capping the ends.
    public void SetPoints(Vector3[] points, float radius, Color col)
    {
        if (points.Length < 2) return;
        vertices = new TubeVertex[points.Length+2];
     
        Vector3 v0offset = (points[0]-points[1])*0.01f;
        vertices[0] = new TubeVertex(v0offset+points[0], 0.0f, col);
        Vector3 v1offset = (points[points.Length-1] - points[points.Length-2])*0.01f;
        vertices[vertices.Length-1] = new TubeVertex(v1offset+points[points.Length-1], 0.0f, col);
     
        for (int p=0;p<points.Length;p++) {
            vertices[p+1] = new TubeVertex(points[p], radius, col);
        }
    }
}
using UnityEngine;

public class TubeVertex
{
    public Vector3 point;
    public float radius;
    public Color color;
 
    public TubeVertex(Vector3 point, float radius, Color color)
    {
        this.point = point;
        this.radius = radius;
        this.color = color;
    }
}

The source was this page http://wiki.unity3d.com/index.php/3D_Physics_Based_Rope