- Home /
My enemy keeps floating in mid air and need help raycasting!
So when my enemy teleports to a random and when my player is in a building with multiple stories my enemy sometimes teleports outside the wall and is floating in mid air. I've tried raycasting, but it does not seem to work please help. Also it crashes because of my while statement so if someone can explain to me why that would also help a lot. Here is the part of code that I have trouble with.
function seenThroughWall()
{
transform.position=Vector3(Random.Range(XRange,XRange2),
(HRange),Random.Range(YRange,YRange2));
checkIfFloating();
Debug.Log("You saw the enemy through the wall!");
while (grounded == false)
{
Debug.Log("Enemy wasn't grounded!");
transform.position=Vector3(Random.Range(XRange,XRange2),
(HRange),Random.Range(YRange,YRange2));
checkIfFloating();
if (grounded == true)
{
CheckIfVisible();
}
}
}
function checkIfFloating()
{
var hit : RaycastHit;
Debug.DrawLine (transform.position, hit.point, Color.red);
if (Physics.Raycast (theEnemy.position,-Vector3.up , hit))
{
var distanceToGround = hit.distance;
if (distanceToGround > 0.5)
{
grounded = false;
}
else
{
grounded = true;
}
}
}
It would also be nice if you could help with my enemy not teleporting into walls. I am very new to Raycasting.
Not sure this is the issue, but you are setting your position using "transform.position", but you are doing your raycast with "theEnemy.position". You've not included the code that initializes theEnemy.
I changed transform.position to theEnemy.position, but that didn't seem to work.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Enemy AI using raycasting.. **Solved** 2 Answers
Enemy NPC walks through hills 1 Answer