- Home /
Raycast not detecting anything if MainCamera's X rotation is > 41.5
I am using a raycast script to detect rocks/trees in front of my character so I can chop them, etc, but if I look down (so that X rotation of the main camera is more than 41.5) the raycast will detect absolutely nothing. What is causing this error? I am use the exact same script as I used in my Unity 4 version of the game, and I didn't have this problem back then!
This is the script I am using, and yes I know, sound management in the same script. I am kind of a beginner, and I will improve my scripts when I've got basic gameplay features in the game!
#pragma strict
var rayLength : int = 10;
var soundWoodChop1 : AudioClip;
var soundWoodChop2 : AudioClip;
var soundWoodChop3 : AudioClip;
var soundWoodChop4 : AudioClip;
var soundRockPick1 : AudioClip;
var soundRockPick2 : AudioClip;
var soundRockPick3 : AudioClip;
var soundRockPick4 : AudioClip;
var hasAxe : boolean = false;
var hasPick : boolean = false;
var canSwing : boolean = false;
private var randomNumber : int = 0;
private var animController : animationController;
var tree : GameObject;
var rock : GameObject;
function Start()
{
animController = GameObject.Find("gameManager").GetComponent(animationController);
hasAxe = true;
hasPick = false;
canSwing = true;
}
function Update()
{
var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay (transform.position, forward, Color.green);
if (animController.canSwing)
{
canSwing = true;
}
else
{
canSwing = false;
}
if (Input.GetKeyDown(KeyCode.F))
{
hasAxe = true;
hasPick = false;
}
else if (Input.GetKeyDown(KeyCode.G))
{
hasPick = true;
hasAxe = false;
}
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if (hit.collider.gameObject.tag == "Tree" && hasAxe)
{
tree = (hit.collider.gameObject);
animController = GameObject.Find("gameManager").GetComponent(animationController);
if (Input.GetButtonDown("Fire1") && canSwing)
{
canSwing = false;
playWoodChopSound();
tree.GetComponent(TreeController).treeHealth -= 1;
}
}
else if (hit.collider.gameObject.tag == "Rock" && hasPick)
{
Debug.Log("you are looking at a rock");
rock = (hit.collider.gameObject);
animController = GameObject.Find("gameManager").GetComponent(animationController);
if (Input.GetButtonDown("Fire1") && canSwing)
{
canSwing = false;
playRockPickSound();
rock.GetComponent(rockController).rockHealth -=1;
}
}
}
}
function playRockPickSound()
{
randomNumber = Random.Range(1,5);
if (randomNumber == 1)
{
GetComponent.<AudioSource>().PlayOneShot(soundRockPick1);
}
if (randomNumber == 2)
{
GetComponent.<AudioSource>().PlayOneShot(soundRockPick2);
}
if (randomNumber == 3)
{
GetComponent.<AudioSource>().PlayOneShot(soundRockPick3);
}
if (randomNumber == 4)
{
GetComponent.<AudioSource>().PlayOneShot(soundRockPick4);
}
}
function playWoodChopSound()
{
randomNumber = Random.Range(1, 4);
if (randomNumber == 1)
{
GetComponent.<AudioSource>().PlayOneShot(soundWoodChop1);
}
else if (randomNumber == 2)
{
GetComponent.<AudioSource>().PlayOneShot(soundWoodChop2);
}
else if (randomNumber == 3)
{
GetComponent.<AudioSource>().PlayOneShot(soundWoodChop3);
}
else if (randomNumber == 4)
{
GetComponent.<AudioSource>().PlayOneShot(soundWoodChop4);
}
}
function Wait()
{
yield WaitForSeconds(0.2);
}
Answer by Harald921 · May 04, 2015 at 09:28 AM
Answer to my own question, the raycast detected the characters collider. Put the character in a layer that ignores raycasts and it will work!
Your answer
Follow this Question
Related Questions
Raycast consuming too much ! (VR) 2 Answers
in rewarded ad which code means user closed the ad by itself 2 Answers
Using Ray cast for click to move 2 Answers
Camera switch between child back to main camera issue 2 Answers
Vertical distance indication 1 Answer