- Home /
The question is answered, right answer was accepted
my tree felling script isnt doing as i want
hi there this is my first time trying this,I want to have a tree that is choppable and my problem is that my script bellow doesn't add force correctly here's the code :
using UnityEngine;
using System.Collections;
public class felltree : MonoBehaviour
{
public int Treehealth;
public GameObject Treeprefab;
public GameObject Treestump;
public GameObject Treetrunk;
public Transform holder;
// Use this for initialization
void Start ()
{
Treehealth = 6;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown ("space")) {
Treehealth -= 2;
if (Treehealth <= 0) {
Treehealth = 0;
Felltree ();
Debug.Log ("felling tree");
}
}
}
void Felltree ()
{
Instantiate (Treestump, Treeprefab.transform.position + new Vector3 (0.1f, 0, 0), Treeprefab.transform.rotation);
Instantiate (Treetrunk, Treestump.transform.position, Treeprefab.transform.rotation);
Destroy (Treeprefab);
Treetrunk.GetComponent<Rigidbody> ().isKinematic = false;
Treetrunk.GetComponent<Rigidbody> ().AddForceAtPosition (new Vector3 (200f, 0, 0),new Vector3(0,1f,0));
Debug.Log ("adding force to tree");
}
}
how this is supposed to work is I have a full tree ,a stump and a tree trunk that I made in blender, the script replaces the full tree with the stump and trunk objects and adds force to the trunk to make it fall over so that eventually it can be processed by a settler, I'm obviously doing something(if not everything) wrong,
here's a screenshot of the hierarchy showing the script attached to the tree
http://imageshack.com/a/img673/937/3xFcHW.png
heres one of the script in action
http://imageshack.com/a/img538/9983/bAtpl3.png
and here is the console during the last image
http://imageshack.com/a/img661/8564/bp64ID.png
thank you in advance for your help
Answer by 10chaos1 · Aug 24, 2015 at 04:08 AM
hey ive solved the problem i completely re wrote the script and this is the new script:
using UnityEngine;
using System.Collections;
public class Chop : MonoBehaviour
{
public Transform Holder;
public GameObject Stump;
public GameObject Trunk;
GameObject clone;
GameObject treeclone;
public static int health = 5;
// Use this for initialization
void Start ()
{
treeclone = GameObject.Find ("tree_low");
}
// Update is called once per frame
void Update ()
{
if (health <= 0) {
health = 0;
Timber ();
}
}
void Timber ()
{
if (treeclone != null) {
Instantiate (Stump, treeclone.transform.position, treeclone.transform.rotation);
clone = Instantiate (Trunk, Stump.transform.position, Stump.transform.rotation)as GameObject;
clone.transform.GetComponent<Rigidbody> ().isKinematic = false;
DestroyImmediate (treeclone);
}
}
}
this does what i originally intended and i hope this helps others with their game development