- Home /
Raycasting in script suddenly stopped working
Hello again my friends! So I had my code working for like 1 hour ago. And now when I start unity again my raycasting script doesn't detect my enemies anymore. And I have no idea on what to do because the code looks fine to me. I can't find any flaws. Here's the code if you want to take a look. Thank you for your help. (And I'm making a PvZ styled game FYI)
var A : Animator;
var EIsNear : boolean;
var Arrow : GameObject;
var ArrowPoint : Transform;
var hit : RaycastHit;
var Reach : float = 35.0;
function Update () {
Rayy();
if (EIsNear == true)
{
A.SetBool("isAttacking", true);
}
if (EIsNear == false)
{
A.SetBool("isAttacking", false);
}
}
function Rayy()
{
var fwd = transform.TransformDirection (Vector3.right);
Debug.DrawRay(transform.position, fwd * Reach, Color.red);
if (Physics.Raycast (transform.position, fwd, Reach)&&hit.transform == GameObject.FindWithTag("Enemy"))
{
EIsNear = true;
}
else
{
EIsNear = false;
}
}
Answer by Swagathaur · Nov 01, 2015 at 10:58 PM
By the looks of things your Rayy function doesnt actually change Hit. If you go to the line which calls the Physics.Raycast function and have a look at the other overloads for that function, you will see one which also takes in a RayCastHit variable. You need to add "out hit" to get it to work.
like this (I think)
Physics.Raycast(transform.position, fwd, out hit, Reach);