- Home /
Tree Chopping Script Issues
Hey guys! I am having issues with the script attached to this question and i was wondering what those issues are. It's been bugging me for a bit now, It doesn't want to work so i'm willing to accept any feedback at the moment - I'm new to UnityJava so I'm having issues here and there!
This script is just meant to make the player cut down a tree, but it doesn't want to function correctly and it's pretty much halting progress for us right now.
var Sphere : GameObject;
var duration : float = 1;
private var ShowGUI : boolean = false;
private var Cutting : boolean = false;
private var startTime : float = 0;
function OnMouseOver (){
var player = GameObject.Find("Player");
if (Vector3.Distance(GameObject.Find("Player").transform.position, transform.position) > 2.0)
return;
ShowGUI = true;
if (Input.GetMouseButtonDown(0)){
Cutting = true;
startTime = Time.time;
}
if (Cutting){
if (Time.time - startTime < duration){
Instantiate(Sphere, transform.position, transform.rotation);
}
else{
Cutting = false;
Destroy(gameObject);
}
}
}
function OnMouseExit(){
ShowGUI = false;
}
function OnGUI(){
if (ShowGUI)
GUI.Label(Rect (10, 10, 100, 20), "Cut this tree down!");
}
Any help is appreciated! Brayden
Hello Brayden_H What's your problem? Don't be fear about being new to anything :) Also i wrote a script for cutting down trees with physics if you want i can post it but first tell me your error. Thanks.$$anonymous$$irbyRawr
It doesn't actually come up with an error on any of the functions, etc. It just seems to not run on the left mouse click. I tested whether the $$anonymous$$ouseOver was working and it was, So i don't understand why it isn't running. If you need more information, just ask.
Well, why you don't use raycast? it's better than find and you can do a manager. I will take a look at the code. And ins$$anonymous$$d of time, you can use a variable with the life of the tree, for example if the tree is more bigger you can put more life.
I get what you mean, But do you $$anonymous$$d throwing up an example or linking me to a wiki page or something related to that. Sort of working on a progression of things with the current game and i'm sick, so my $$anonymous$$d is failing on me. Haha.
I have one error at the moment.
"$$anonymous$$ Identifier: CutTree"
choque.collider.GetComponent(CutTree).Cutting();
Is that looking for the other game object, or..?
Answer by Kirbyrawr · May 20, 2013 at 08:21 AM
Well, my script is this:
CutTree.js
#pragma strict
var treelife = 100;
var player : Transform;
var treeragdoll : GameObject;
var arbol : GameObject;
var Player2 : GameObject;
private var cut = false;
private var cut2 = true;
function Awake(){
Cutting();
}
function Cutting(){
treelife -=5;
if(treelife <= 0){
var treeragdollClone = Instantiate(treeragdoll, transform.position, transform.rotation);
cut2 = false;
// Kill ourselves
Destroy(gameObject);
}
if(corte2 == false){
print("Do Something here if you want")
}
}
RayCastManager.js
//Private Variables.
private var cut = false;
//GameObject's Variables.
var player : Transform;
var player2 : GameObject;
var playerdisable : GameObject;
var choque : RaycastHit;
function Update() {
//Variables of the RaycastManager.
var cam : Transform = Camera.main.transform;
var rayo = new Ray(cam.position, cam.forward);
//Tree Start Non Raycast!
if(cut == false){
print("Do Something here if you want")
}
//Tree End Non Raycast!
//////////////////////
if (Physics.Raycast(rayo, choque)) {
//Cutting Trees Start
if (choque.collider.tag == "Tree" && choque.distance <= 1.35){
cut = true;
}
else{
cut = false;
}
}
//End Of the Raycast Manager.
//Another Functions!
if (Input.GetMouseButtonDown(0)){
if (cut == true){
choque.collider.GetComponent(CutTree).Cutting();
}
}
Maybe it can have a little errors because i edit it a little. If you have any questions or errors tell me :) And if this question is right answered click on up button ^^
ATTACH THE RAYCAST MANAGER TO YOUR MAIN CAMERA and the OTHER SCRIPT TO A TREE. KirbyRawr.
Thanks heaps $$anonymous$$irbyRawr, Had to fix a couple of things but never-the-less it works!
Hey $$anonymous$$irbyRawr for the scripts you put up here did you use a normal tree from the terrain editor or did u make your own.
CutTree.js(28,7): BCE0005: $$anonymous$$ identifier: 'corte2'.
HELP
Answer by minecraft7089 · Sep 08, 2014 at 01:32 PM
It says "unknown identifier : corte2.
should be cut2 by the looks of things I haven't used this to try it but I'm pretty sure :)