- Home /
my ray cast is only shooting out a few meters no matter the distance.
my ray cast is only shooting out a few meters no matter the distance. currently distance is at a 1000 units( i should say) the pic is yellow is he ray cast line drawn. the second pc i believe it is in blue
I have a function and it has been assigned to the update function. when i debug it, it does tell me that the range is current at 1000, but the ray only shoots a few meters. below is the function, and below that is the update function that may or may not be temporary. although the function ShootContrllor is displayed first, it is actually below, a few other functions down.
please be advised that i watched the unity answers video, and i have done accordingly. please do the same and remember this is unity answers and not unity forum. In particular pointing me to some reference on raycasting, or .....you know.
public void ShootController()
{
currentWeapon.shotTimer = Time.deltaTime + currentWeapon.firerate;
Debug.Log("I shot");
RaycastHit hit;
if(Physics.Raycast(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward), out hit,currentWeapon.weaponRange))
{
Debug.DrawRay(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward),Color.blue,currentWeapon.weaponRange);
if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)
{
Debug.Log("I hit the Head");
Debug.Log("Range is" + currentWeapon.weaponRange);
}
else if(hit.collider.gameObject.name == GameObject.FindWithTag("BodyCollider").gameObject.name)
{
Debug.Log("I hit the Body");
Debug.Log("Range is" + currentWeapon.weaponRange);
}
else
{
Debug.DrawRay(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward),Color.yellow,currentWeapon.weaponRange);
//Debug.Log("I hit something");
}
}
}
public void Update()
{
if(currentWeapon.shotTimer > 0)
{
currentWeapon.shotTimer -= Time.deltaTime;
}
if(currentWeapon.shotTimer < 0)
{
currentWeapon.shotTimer = 0;
}
if(Input.GetMouseButtonDown(0) && currentWeapon.bulletsLeft > 0)
{
if(currentWeapon.shotTimer == 0)
{
ShootController();
}
Bullets();
}
}
Thanks for leading me to the error, although the docs were trial and error. they are written for professionals by professionals and aren't detailed enough for the layman. i got it though.......... thumbs up
Vector3 forward = currentWeapon.SpawnPoint.TransformDirection(Vector3.forward);
if(Physics.Raycast(currentWeapon.SpawnPoint.position, forward * currentWeapon.weaponRange, out hit,currentWeapon.weaponRange))
{
Debug.DrawRay(currentWeapon.SpawnPoint.position,forward * currentWeapon.weaponRange,Color.blue,currentWeapon.weaponRange);
Answer by Julien-Lynge · Mar 15, 2013 at 05:57 PM
Here is your error:
Debug.DrawRay(currentWeapon.SpawnPoint.position, currentWeapon.SpawnPoint.TransformDirection(Vector3.forward),Color.yellow,currentWeapon.weaponRange);
You are drawing a ray that is only 1 unit long (Vector3.forward is a unit vector).
Please see the script reference:
http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html
Your answer
Follow this Question
Related Questions
RayCast Problem : 2d 1 Answer
Using RayCast to do Continuous Shooting? 1 Answer
Turret With Raycast Camera 1 Answer
not losing ammo when shooting 1 Answer