Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tree fall Problem (Script)

Discussion in 'Scripting' started by surya91, Feb 14, 2016.

  1. surya91

    surya91

    Joined:
    Feb 6, 2016
    Posts:
    8
    hello, i hope this is the right place for my problem.

    i have made the script for my tree and create a prefab for my tree with tree creator.

    this is the script : code updated. finally, it work, and then after 5 second (i think) the tree gone.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class pohonjatuh : MonoBehaviour {
    6.  
    7.     public int Health;
    8.     public float Force;
    9.     public GameObject fallenTree;
    10.     public Camera myCamera;
    11.     public float treeGone; //updated. this is the time which i can set.
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         myCamera = GameObject.FindObjectOfType<Camera> ();
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if (Health > 0) {
    21.             GetComponent<Rigidbody> ().isKinematic = true;
    22.             if (Vector3.Distance (transform.position, myCamera.transform.root.transform.position) < 10f) {
    23.                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
    24.                     Ray ray = new Ray (myCamera.transform.position, myCamera.transform.forward);
    25.                     RaycastHit hit;
    26.                     if (Physics.Raycast (ray, out hit, 10f)) {
    27.                         if (hit.collider.gameObject == gameObject) {
    28.                             --Health;
    29.                         }
    30.                     }
    31.                 }
    32.             }
    33.         }
    34.  
    35.         if (Health <= 0) {
    36.             Health = 0;
    37.             GetComponent<Rigidbody> ().isKinematic = false;
    38.             GetComponent<Rigidbody> ().AddTorque (myCamera.transform.right * Force, ForceMode.Impulse);
    39.             Destroy (gameObject, treeGone);
    40.             }
    41.         }
    42.     }
    43.  

    update video :



    thank you, now i'll try next step : spawn a tree. spirit
    :( i try it from the morning until this time :D never give up.
     
    Last edited: Feb 14, 2016
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    the rigidbody might be asleep.
     
  3. surya91

    surya91

    Joined:
    Feb 6, 2016
    Posts:
    8
    well, can you explain why rigidbody might be asleep?
    :D


    update : i'm success make it fall after hit. but, i must push it. example : game start -> i push the tree before hit, the tree success for standing (use isKinematic true). then after i hit it 4 - 5 times, the tree must be pushed (with my body FPS) to make it fall (thats the problem). what i need is, after i hit it 4- 5 times, the tree fall without my push help.

    i had updated the code. hope someone can solve this. see the comment code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pohonjatuh : MonoBehaviour {
    5.  
    6.     public int Health;
    7.     public float Force;
    8.     public GameObject fallenTree;
    9.     public Camera myCamera;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         myCamera = GameObject.FindObjectOfType<Camera> ();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.         if (Health > 0) {
    19.             GetComponent<Rigidbody>().isKinematic = true; //this is new  update
    20.             if (Vector3.Distance (transform.position, myCamera.transform.root.transform.position) < 10f) {
    21.                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
    22.                     Ray ray = new Ray (myCamera.transform.position, myCamera.transform.forward);
    23.                     RaycastHit hit;
    24.                     if (Physics.Raycast (ray, out hit, 10f)) {
    25.                         if (hit.collider.gameObject == gameObject) {
    26.                             --Health;
    27.                         }
    28.                     }
    29.                 }
    30.             }
    31.         }
    32.  
    33.         if (Health <= 0) {
    34.             Health = 0;
    35. GetComponent<Rigidbody>().isKinematic = false; // this is new update.
    36. GetComponent<Rigidbody>().AddTorque (myCamera.transform.right * Force, ForceMode.Impulse);  // i move this code here. so it will be executed before Destroy gameObject. // but it seem, i must push the tree, so it will fall with my help.
    37.             Destroy (gameObject);
    38.             Instantiate (fallenTree, transform.position, transform.rotation);
    39.         }
    40.     }
    41. }
    42.  
    should i update the first script up there? :D
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    The script doesnt seem to do what you think it does. i have no idea what fallenTree is supposed to be but I'm guessing its the identical tree but able to be moved via physics.

    in this part:
    Code (CSharp):
    1.         if (Health <= 0) {
    2.             Health = 0;
    3. GetComponent<Rigidbody>().isKinematic = false; // this is new update.
    4. GetComponent<Rigidbody>().AddTorque (myCamera.transform.right * Force, ForceMode.Impulse);  // i move this code here. so it will be executed before Destroy gameObject. // but it seem, i must push the tree, so it will fall with my help.
    5.             Destroy (gameObject);
    6.             Instantiate (fallenTree, transform.position, transform.rotation);
    7.         }
    you destroy the object immediately after applying the torque to it, and replace it with fallenTree most likely. so the object you want physics interactions with is not this object but fallenTree. I guess you should try applying torque to fallenTree instead.
     
  5. surya91

    surya91

    Joined:
    Feb 6, 2016
    Posts:
    8
    i'll be back with some video about the tree, so we all can see the detail. well, last time i used the standard tree prefab. let's see if i delete the code about Destroy and Instantiate. thanks laperen


    update, i put the video on my first post, enjoy the short video...
     
    Last edited: Feb 14, 2016