- Home /
Why isn't raycast hitting when going left.
My right raycast works fine but the left one never hits anything and It's starting to get frustrating. What am I missing here? The right and left one are identical aside from the direction of the vector3 and the dir. distToSide = GetComponent().bounds.extents.x;
case EnemyType.Jump:
if (!myHealth.invulnerable)
{
if (dir == 1)
{
Debug.DrawRay(new Vector3(transform.position.x, transform.position.y, transform.position.z), Vector3.right, Color.green);
if (Physics.Raycast(transform.position, Vector3.right, distToSide * dir, ignoreLayers))
{
if (!waitingToFlip)
{
StartCoroutine("WaitToFlip");
}
}
if (!jumping)
{
rB.AddForce(new Vector3(dir * 225, 300, 0));
jumping = true;
StartCoroutine("Cooldown");
}
}
else
{
Debug.DrawRay(transform.position, Vector3.left, Color.green);
if (Physics.Raycast(transform.position, Vector3.left, distToSide * dir, ignoreLayers))
{
if (!waitingToFlip)
{
StartCoroutine("WaitToFlip");
}
}
if (!jumping)
{
rB.AddForce(new Vector3(dir * 225, 300, 0));
jumping = true;
StartCoroutine("Cooldown");
}
}
}
return;
}
}
private IEnumerator WaitToFlip()
{
waitingToFlip = true;
yield return new WaitForSeconds(.1f);
dir *= -1;
waitingToFlip = false;
}
private IEnumerator Cooldown()
{
yield return new WaitForSeconds(.5f);
yield return new WaitUntil(() => IsGrounded() == true);
rB.angularVelocity = Vector3.zero;
rB.velocity = Vector3.zero;
jumping = false;
}
distToSide should always be positive (you can't be -5m away from a wall, same as something can't weight -30lbs).
Could you add a Debug right after the left Raycast check? Is the rat being drawn in the proper direction?
Answer by bobisgod234 · May 15, 2018 at 01:28 AM
Is "dir" expected to be -1 when moving left? If so, then you multiply "distToSide" by a negative number, which is probably not what you intended.
Try replacing "distToSide * dir" with just "distToSide"
Don't I want disToSide to be negtive so that it goes left or no. Also removing it didn't change anything.
Actually it turns out you are correct. I had accidentally changed the second raycast to be right also while experimenting for a solution. $$anonymous$$ake it an answer so I can accept, thank you!
Answer by plindsey · May 15, 2018 at 02:11 AM
You want the world left or the objects left? If you want the objects left there is no transform.left you need to use transform.right *-1
For world left maybe try Vector3.right * -1 instead
Your answer
Follow this Question
Related Questions
'height' is not a member of 'UnityEngine.Collider'. 1 Answer
How should I make a custom suspension system? 1 Answer
Touching Falling Objects 1 Answer
Creating a teleportation gun 1 Answer