Trying to program lantern to sway in the wind?

Hello. I am a 3D Modeler in Unity and I created a lantern with a chain. Each link has a hinge joint, rigid body and a collider.
alt text

I am trying to program the lantern to naturally sway in the wind? I am not a programmer by any means, but my teacher said to create a basic code to apply a force to the rigid bodies, and I created a code that looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class LanternForce : MonoBehaviour
{
    public float thrust;
    public Rigidbody rb;
 
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
 
    void FixedUpdate()
    {
        rb.AddRelativeForce(Vector3.forward * thrust);
    }
}

The code works, but the lantern is blown to the ceiling and staying there.
alt text
alt text
According to my teacher, the lantern is having a force constantly applied to it, making blown onto the ceiling every second. But yet she wouldn’t tell me how to fix it.

How can I modify the code to have a random force applied to it, and to make it blow “sideways” instead of “forward”, to create a natural swaying motion in the wind?

For the simplest approach, while your game loop is running, calculate the value of Sin() or Cos() which should give you a negative or positive value as it sways back and forth. Clamp the value between -1 and 1. You should at least be able to push the lantern back and forth, even if it only is in one direction. (Edit, I didn’t mean to sound cryptic, use the negative and positive value of the sinusoid to affect your force vector, so it goes back and forth).

Toss in some randomness and you can change the position of the vector around the base of the lantern to which force is applied and add a damping factor to the -1 and 1 value used to multiply your force vector. The randomness would be at the point of the lantern you put the force, as well as how much force (you can increase its magnitude or decrease it) and apply force for longer or shorter periods of time.

I would add variables to your script to play with these values, so you know how to constrain the maximum amount of force applied to the lantern to make it look like a gentle wind is blowing and not an eternal hurricane. :slight_smile: