Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by devcutters · Oct 04, 2014 at 10:41 PM · raycastcollider

raycasthit questions

hi, i am trying to make some choppable trees my raycast do land on the tree and do detect it but i cant seem to remove health from it. My tree has a rigid body and a box colider. Any help would be appreciated , if you need more information or post isn't complete enought please let me know.

 #pragma strict
 
 var rayLength = 10;
 
 
 //private var treeScript : TreeController;
 
 var tree : GameObject;
 
 private var playerAnim : PlayerControl;
 
 function Update()
 {
     var hit : RaycastHit;
     var fwd = transform.TransformDirection(Vector3.forward);
     
     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
     {
         if(hit.collider.gameObject.tag == "Tree")
         {
             //treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent(TreeController);
             tree = (hit.collider.gameObject);
             playerAnim = GameObject.Find("@Idle").GetComponent(PlayerControl);
             
             if(Input.GetButtonDown("Fire1") && playerAnim.canSwing == true)
             {
                 //treeScript.treeHealth -= 1;
                 tree.GetComponent(TreeController).treeHealth -= 1;
             }
         }
     }
 }

 //this script is on the tree and is called TreeController

 var treeHealth : int = 5;
 
 var logs : Transform;
 var branch : Transform;
 var tree : GameObject;
 
 var speed : int = 8;
 
 function Start()
 {
     tree = this.gameObject;
     rigidbody.isKinematic = true;
 }
 
 function Update()
 {
     if(treeHealth <= 0)
     {
         rigidbody.isKinematic = false;
         rigidbody.AddForce(transform.forward * speed);
         DestroyTree();
     }
 }
 
 function DestroyTree()
 {
     yield WaitForSeconds(7);
     Destroy(tree);
     
     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
     Instantiate(branch, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(branch, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(branch, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
 } 


Comment
Add comment · Show 1
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
avatar image Benproductions1 · Oct 05, 2014 at 12:59 AM 0
Share

Please, for the love of performant code, never use GameObject.Find in Update or FixedUpdate. Do you get any exceptions thrown? What have you tried?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Downstream · Oct 05, 2014 at 01:26 AM

It works for me.

Comment
Add comment · Show 1 · 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
avatar image devcutters · Oct 05, 2014 at 03:44 AM 0
Share

did i miss anything ?

avatar image
0

Answer by Omegathorion · Oct 05, 2014 at 04:27 AM

It looks like working code. I think what's happening is that nothing happens to the tree when it loses health. So even though you are technically removing health, nothing happens as a result. Throw this code snippet into your TreeController code:

 function Update() {
     if (treeHealth <= 0) {
         Destroy(gameObject);
     }
 }

That basically means that if the tree were to ever be reduced to 0 health, it will destroy itself.

Comment
Add comment · Show 3 · 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
avatar image Downstream · Oct 05, 2014 at 04:31 AM 0
Share

I don't think it's a good idea to do that in the update loop, it's a tree after all and there are lots of them, rather use a function which will only check when the tree is damaged somehow.

avatar image Omegathorion · Oct 05, 2014 at 05:13 AM 0
Share

That's a good point to bring up, I just wrote it that way so that it would be easy to copy and paste it right into his code. You're right that it's better for the trees to have some kind of function to receive damage, but that would require a (slightly) different architecture setup than what devcutters has now.

avatar image devcutters · Oct 05, 2014 at 07:00 AM 0
Share

here is my function update i didn't thought it would be important to include it.it. I'm trying to make a basic resources extraction so i can expand later in it.For the other architecture basicly i would have to set it with a tag and if its a tree and a axe is equipped then damage would be applied ?

so here is my function update and DestroyTree from my TreeController script

 function Update()
 {
     if(treeHealth <= 0)
     {
         rigidbody.is$$anonymous$$inematic = false;
         rigidbody.AddForce(transform.forward * speed);
         DestroyTree();
     }
 }
 
 function DestroyTree()
 {
     yield WaitForSeconds(7);
     Destroy(tree);
     
     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
     
     Instantiate(branch, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
     Instantiate(branch, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
     Instantiate(branch, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Remove object with specific object 1 Answer

Accessing colliders, in if statement 2 Answers

What is the best way of breaking boxes 1 Answer

Melee-like function? 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