- Home /
Stop raycasting for close distance
Hello everyone, I have my raycast shooting code but I want it not to raycast to the collisions which is too close to player. I want it because I'm using Camera.main.ScreenPointToRay so it sometimes collides with itself, let me explain with some pics :
I mean when I look down it collides with itself and particles are being seen very ridiculusly, so is there anything I can do for not to Raycast too close ?
Here is my code :
function Start(){
hitParticles = gun.GetComponentInChildren(ParticleEmitter);
if (hitParticles)
}
function Update(){
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2));
if(Input.GetMouseButton(0)&& Time.time > nextFire){
nextFire = Time.time + fireRate;
if (Physics.Raycast(ray,hit,100)){
Debug.DrawLine( transform.position, hit.point, Color.red);
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.LookRotation(hit.normal);
hitParticles.Emit();
}
}
}
}
Answer by mpavlinsky · Jan 07, 2012 at 05:01 PM
You can just check the distance property of the RaycastHit and not do anything if it's too short. http://unity3d.com/support/documentation/ScriptReference/RaycastHit-distance.html
Your answer
Follow this Question
Related Questions
different particle on enemy second hit 0 Answers
Instantiate diffent particle for different object 1 Answer
Particle Instantiation problem. 0 Answers