How to add rotation to game object without interfering with physics

Hi everyone,

I am using Unity to model residential building failures under hurricane wind and water loading. I currently have a script that reads a text file and if a certain value in that text file is equal to zero then the roof cover component (3D cube game object) fails. When this happens, the isKinematic on the rigidbody for that game object is set to false so that it will fly off in simulated wind. Where I am having trouble is I want to add a rotation in the z axis to make the failure and effect of flying through the wind more realistic. However, I am unable to make the game object rotate in the z direction. I would think it would be a simple transform.Rotate but this is not working. Do you have any suggestions or see any problems with what I am doing? I have attached the code I have applied to the game object below. Thank you for your help!

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Roof_Cover_13 : MonoBehaviour
{
    public string txtFile = "Roof_Cover_Failure";
    string txtContents;
    int[] stress;
    Rigidbody rb;


    // Start is called before the first frame update
    void Start()
    {
        System.Random rnd = new System.Random();
        int delay = rnd.Next(5, 30);
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = true;
        char[] delimiterChars = { '\n' };
        txtContents = File.ReadAllText(@"C:\Users\giova\Dropbox\Research\Graduate School\Roof_Cover_Failure");
        string[] splitTxt = txtContents.Split(delimiterChars);
        stress = new int[splitTxt.Length];

        for (int j = 0; j < splitTxt.Length; j++)
        {
            stress[j] = System.Convert.ToInt32(splitTxt[j]);
        }

        Invoke("Failed", delay);

    }

    void Failed()
    {
        if (stress[85] == 0)
        {
            rb.isKinematic = false;
            transform.Translate(0.3f, 0.2f, 0);
        }
    }

    private void LateUpdate()
    {
        if (rb.isKinematic == false)
        {
            transform.Rotate(0, 0, -1);
        }
    }

}

All that does is rotate it by that much right now, this frame, never again.

Instead, set the .angularVelocity property of the Rigidbody:

https://docs.unity3d.com/ScriptReference/Rigidbody-angularVelocity.html

NOTE: to keep it from banging against the roof instantly as it rotates, you may need to gradually ramp up the rotation as it peels away, otherwise it might look weird.

Of course a real roof torn off in a hurricane is just an airfoil experiencing lift, drag and torque, which (may) cause it to begin spinning. Simulating that is totally possible, but not trivial in the general sense, as obviously local airflow around the roof would be strongly influenced by the building shape, which of course is further changing as it pulls apart. :slight_smile:

1 Like

Thank you, this worked!