- Home /
Raycast does not detect Objects
Hello unity community.
to cut to the chase what I am doing is I am trying to make a parkour system and what it needs to do is detect if a wall it "X" amount away from the player. I have a raycast to check if the player is grounded and that works perfectly. That is why I think I dont know what is wrong.... So the problem that is happening is that I have a raycast shooting to the right of the player and this raycast does not detect gameobjects. Please any help is welcomed.
here is my parkour script
//public
public GameObject RaycastShooter;
public float ParkourIfLessThan = 1f;
public float WallRunSpeed = 9.75f;
//Distances
public float WallDistance = 1f;
//Bools
private bool CanWallRun;
private bool IsWallRunning;
//Raycast
private Vector3 RightRay = new Vector3 (1,0,0);
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//All the different Raycast hit
RaycastHit RightHit;
RaycastHit LeftHit;
if(Physics.Raycast(RaycastShooter.transform.position, RightRay.normalized, out RightHit, WallDistance))
{
float DistanceToWall = RightHit.distance;
if(DistanceToWall <= ParkourIfLessThan)
{
print("can wall run");
}
if(DistanceToWall > ParkourIfLessThan)
{
print("Can't wall run");
}
Debug.DrawLine(RaycastShooter.transform.position, RightHit.point);
}
}
Just tested this on my PC and it works great :D thank you for your help.
Answer by robertbu · Apr 16, 2014 at 03:30 AM
Note that RightRay is the world right. For most 3D games, you want to raycast from the right of the object. Something like:
if(Physics.Raycast(RaycastShooter.transform.position, RaycastShooter.transform.right, out RightHit, WallDistance))
Since I don't know your game mechanic, I'm not sure this is your problem. Also you might want to do a Debug.DrawRay() with exactly the same parameters as your Raycast() so you can visualize any Raycast().
Answer by drudiverse · Dec 05, 2015 at 09:41 AM
Check the distance of the raycast. i.e. 100 meters may only capture half the objects, prblem i had, in physics.raycast() function