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 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.
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?
Im pretty sure you can achieve this by using some joints, along with applying a force to the lamp itself. I would say Hinge joints but I am not sure. I assume you want to have the chain sway with it?
Also, as your teacher said - do not apply the force constantly, but only sporadically. To make it look even more natually, you should apply a force that goes up and down organically over time. Unity provides such a function via Perlin Noise, one of the most important functions for quasi-randomness that also Looks very organic.
You’d better not mess with joints at all. Multijoint systems aren’t stable. Google for unity3d rope problem if you want the detail. Try using cloth system for that, it works much better than physics engine.
What you’re currently simulating is a fan constantly blowing on it from one direction (you’re only adding force on vector3.forward, a shorthand for positive Z), more like a turbo-jet if it’s sticking to the ceiling, try a lower force (lower your “trust” variable).
given the right amount of force the object should rise a little to one side, pulled down back due to gravity, sway the other way because of momentum(?), and start oscillating back and forth, the fan adding energy to it when it sways away and removing energy when it sways to it.
You could change the wind direction in any number of ways as mentioned above with perlin noise or one of the unitSpheres in the Random class for example to give to a more organic feel and no look like it’s next to a fan.
Isn’t that for the terrain grass?
(dunno, never used it)