Hi, I'm very new to Unity and I'm currently trying to make my first Unity game, which is a survival game à la stranded on an island and I have a big problem:
As of now the Character (First Person Controller) should be able to chop down trees, but it's not always working. The Tree is just a standard 3D Model with a Box Collider just as big as its trunk. The Purpose of it is so that I can check what I hit with a RayCast and if it's a tree, then the tree's health should be reduced.
Unfortunately though it's not always working, I've already tried going closer to/farther away from the tree and also moving the camera up and down, but most of the times nothing happens.
Here is the code of the Script used for Chopping the tree:
You might be wondering what PlayControl is, but it's just a Script for the Player Controller, which allows me to swing my axe around to chop down trees.
While it may not be necessary, I'll also post its code here:
Edit: Whenever I remove the RayCast-Script from the Main Camera and add it there again, then the Tree Chopping suddenly works completely fine. However when I save it, close the scene and open it again, the problem pops up again.
I've already tried looking around in the internet, but I haven't found anything which might help me out, and I would like to have it fixed.
Please help.
As of now the Character (First Person Controller) should be able to chop down trees, but it's not always working. The Tree is just a standard 3D Model with a Box Collider just as big as its trunk. The Purpose of it is so that I can check what I hit with a RayCast and if it's a tree, then the tree's health should be reduced.
Unfortunately though it's not always working, I've already tried going closer to/farther away from the tree and also moving the camera up and down, but most of the times nothing happens.
Here is the code of the Script used for Chopping the tree:
RayCast Script :
Code:
var rayLength : int = 10;
private var playerAnim : PlayControl;
var tree : GameObject;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "TreeChoppable")
{
tree = (hit.collider.gameObject);
playerAnim = GameObject.Find("FPSArms_Axe@Idle").GetComponent(PlayControl);
if(Input.GetButtonDown("Fire1") && playerAnim.canSwing == true)
{
tree.GetComponent(TreeController).treeHealth -= 1;
}
}
}
}
You might be wondering what PlayControl is, but it's just a Script for the Player Controller, which allows me to swing my axe around to chop down trees.
While it may not be necessary, I'll also post its code here:
PlayControl Script :
Code:
#pragma strict
private var hasAxe : boolean = false;
var canSwing : boolean = true;
private var isSwinging : boolean = false;
var swingTimer : float = 0.7;
private var controller : CharacterController;
private var playerGUI : PlayerGUI;
function Start()
{
hasAxe = true;
controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
playerGUI = GameObject.Find("First Person Controller").GetComponent(PlayerGUI);
}
function Update()
{
//IDLE SECTION
if(controller.velocity.magnitude <= 0 && isSwinging == false)
{
animation.Play("Idle");
animation["Idle"].wrapMode = WrapMode.Loop;
animation["Idle"].speed = 0.2;
}
//SPRINT SECTION
if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
animation.Play("Sprint");
animation["Sprint"].wrapMode = WrapMode.Loop;
}
//AXE SWINGING SECTION
if(hasAxe == true && canSwing == true)
{
if(Input.GetMouseButtonDown(0))
{
//Reduce Stamina
playerGUI.staminaBarDisplay -= 0.1;
//SWINGING ANIMATION
animation.Play("Swing");
animation["Swing"].speed = 2;
isSwinging = true;
canSwing = false;
}
}
if(canSwing == false)
{
swingTimer -= Time.deltaTime;
}
if(swingTimer <= 0)
{
swingTimer = 1;
canSwing = true;
isSwinging = false;
}
}
Edit: Whenever I remove the RayCast-Script from the Main Camera and add it there again, then the Tree Chopping suddenly works completely fine. However when I save it, close the scene and open it again, the problem pops up again.
I've already tried looking around in the internet, but I haven't found anything which might help me out, and I would like to have it fixed.
Please help.