• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 04, 2015 at 03:34 AM by getyour411 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by 10chaos1 · Aug 10, 2015 at 08:51 AM · c#script.addforcetree

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can i Instantiate on the terrain from left to right ? 0 Answers

Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer

Question about increments 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges