- Home /
raycast cooler
I made a Ai shoot script that works with raycasting but the problem is that the raycast goes non stop and kills me in a second, and i'm trying to find a way to smoth it or at least 1 shot of raycast per second. here is my ai shoot script(feel free to copy if you want i don't mind).
var player : Transform;
var safeDist : float = 15;
var currentDist : float;
var shooting : boolean = false;
var canShoot: boolean = false;
var BloodSplat : GameObject;
var sparkEffect : GameObject;
var gunTip : Transform;
var range : float = 1000;
var force: float = 1000;
var shotDelay : float = 0.5;
var Damage : int = 1;
var Bullet : GameObject;
var ShootTimer : float = 1;
var ShootCooler : float = 10;
function Update () {
currentDist = Vector3.Distance(transform.position, player.position);
if(currentDist < safeDist){
//transform.LookAt(player);
if(!shooting){
shootStuff();
Instantiate(Bullet, gunTip.transform.position, gunTip.transform.rotation);//this just for decoration
}
}
}
function shootStuff(){ // the part that shoot
var hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * range , Color.yellow);
if(Physics.Raycast(transform.position , DirectionRay , hit , range)){
var particleClone1 = Instantiate( sparkEffect , hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone1.gameObject, 0.2);
if ( hit.rigidbody){
hit.rigidbody.AddForceAtPosition( DirectionRay * force , hit.point);
hit.collider.SendMessageUpwards ("HeroeHit" , Damage , SendMessageOptions.DontRequireReceiver);
if( hit.transform.tag == "Player" ) {
var particleClone = Instantiate( BloodSplat, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 0.2);
}
}
}
}
Answer by Soos621 · Apr 29, 2015 at 04:03 PM
You can either take the bullet when you instantiate it and add a force in the direction your ray at is shooting, then on the bullet put a script that tells the object you are shooting to destroy itself
Or
You can use a Coroutine to raycast out then yield return new waitforseconds to have it wait to "wait" to destroy the object
Answer by seb-lopez · Apr 30, 2015 at 08:29 PM
how can i coroutine my raycast? and is it possible to make it burst with this systeme ?
Your answer
Follow this Question
Related Questions
Another Raycast Issue. 0 Answers
Finding RayCastHit's Origin Position 2 Answers
How to Fix a Guns Firing Script so it Doesn't Constantly Fire 3 Answers
Can See Target Not Working 1 Answer
Circle around enemy, 1 Answer