Home > Back-end >  sandy land interact with the object
sandy land interact with the object

Time:08-15

I want to learn a way to make sand that interacts with movement, such as if I throw a ball at it, and it makes an impact on the sand, so I drop a cube on it. It is in the sand when it falls I would be grateful if there were educational videos

CodePudding user response:

To make and in unity, you would need a lot more processing power than we have right now. This is because you would need a rigidbody, which is a type of component, and millons of rigidbodies total to make a large sandy environment.

If you want to do it, you would have to limit yourself the amount of sand.

Here's how I would do it

  1. First, I would make the terrain of the sandy beach (or desert) a mesh, model it in blender (or any other software) or download it.

  2. Import this model and texture it to look like sand. Right now, It won't act like sand (it won't move around when you touch it).

  3. Make a bunch of game objects in a script (let's say 100-1000 depending on how high quality and depth you want it to be. Add rigidbody components to them.

  4. Teleport them to always be in a simulation box, so not all sand is being calculated at once. Learn more by googling (object pooling in unity)

  5. Texture the sand and you should be done.

I will try to make a script for you, but I have never done this before.

  1. Make a script called "sand_sim"

  2. Put "sand_sim" on the terrain model

using UnityEngine;
public class sand_sim : MonoBehaviour
{
   public string beachTag;
   public int sandAmount;
   public float sandRadius;
   public float simDist;
   public GameObject player;

   GameObject[] sand;

   void Start()
   {
      for (int i = 0; i < sandAmount; i  )
      {
         GameObject gam = new GameObject(i.ToString());
         gam.transform.parent = transform;
         gam.Layer = LayerMask.NameToLayer("Ignore Raycast");
         gam.transform.scale = Vector3.one * sandRadius;
         gam.AddComponent<Rigidbody>();
         gam.AddComponent<SphereCollider>();
      }
   }
   void Update()
   {
      if (Physics.Raycast(player.transform.position, Vector3.down, out hit)
      {
         if (hit.transform.gameObject.CompareTag(beachTag))
         {
            for (int i = 0; i < sandAmount; i  )
            {
               if (Vector3.Distance(sand[i].transform.position, player.transform.position) > simDist)
               {
                  Vector2 x1 = sand[i].transform.postion.x;
                  Vector2 y1 = sand[i].transform.position.z;
                  Vector2 ox = player.transform.position.x;
                  Vector2 oy = player.transform.position.z;

                  sand[i].transform.position.x = ox - (x1 - ox);
                  sand[i].transform.position.z = oy - (y1 - oy);
               }
            }
         }
      }
   }
}
  • set beachTag to the tag of the Mesh of the terrain where the beach is.
  • set sandAmount to the grains of sand.
  • set sandRadius to how thick the sand is.
  • set simDist to how far around the player to simulate sand.
  • set player to the camera or whatever you are playing as.

Let me know if there are any problems with it.

  • Related