- Home /
Question by
ahmadk · May 24, 2015 at 06:49 AM ·
scripting problemterrainterrain-trees
How do i make this Script work with Terrain Trees.
There are 2 scripts which i use to chop down trees, script one is put on the tree and basically tells it when to spawn the logs and which direction it should fall in, Script 2 however is put on main camera of my FPS Controller. The problem is that even though the script works when iindividually place the trees into the scene, when I use the tree painter on the terrain,(with a prefab with script) the script doesnt work.
These are the scripts:
Script one(on the tree)
#pragma strict
var treeHealth : int = 5;
var logs : Transform;
var coconut : Transform;
var tree : GameObject;
var speed : int = 8;
function Start()
{
tree = this.gameObject;
GetComponent.<Rigidbody>().isKinematic = true;
}
function Update()
{
if(treeHealth <= 0)
{
GetComponent.<Rigidbody>().isKinematic = false;
GetComponent.<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(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
}
Script 2(For the main camera)
#pragma strict
var rayLength = 10;
private var treeScript : TreeController;
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);
playerAnim = GameObject.Find("FPSArms_Axe@Idle").GetComponent(PlayerControl);
if(Input.GetButtonDown("Fire1") && playerAnim.canSwing == true)
{
treeScript.treeHealth -= 1;
}
}
}
}
PLEASE HELP ME
Comment