Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » RayCast not always working

RayCast not always working

Go down  Message [Page 1 of 1]

1 RayCast not always working on Fri Apr 24, 2015 7:09 am


Member [Lv. 0]
Member [Lv. 0]
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:

RayCast Script:

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:

#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()
 if(controller.velocity.magnitude <= 0 && isSwinging == false)
 animation["Idle"].wrapMode = WrapMode.Loop;
 animation["Idle"].speed = 0.2;
 if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
 animation["Sprint"].wrapMode = WrapMode.Loop;
 if(hasAxe == true && canSwing == true)
 //Reduce Stamina
 playerGUI.staminaBarDisplay -= 0.1;
 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.

View user profile

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum