- Home /
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);
}
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?
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.
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.
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.
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
Follow this Question
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