Can someone help me with my Raycast shooting.
Now it shoots kind of randomly, and I want to it shoot exactly straight.
Please help, I’ve tried everything.
var fireRate = 0.02;
var force = 10.0;
static var damage = 25;
var bulletsPerClip = 30;
static var clips = 3;
var range = 100;
static var bulletsLeft : int = 30;
private var hitParticles : ParticleEmitter;
private var nextFireTime = 0.0;
function Start () {
hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
}
function Update () {
if(Input.GetMouseButton(0))
{
Fire();
}
if (bulletsPerClip == 0)
{
Reload ();
}
if (Input.GetKeyDown ("r"))
Reload ();
}
function Fire () {
if (bulletsLeft == 0)
return;
if (bulletsPerClip == 0)
return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
}
function Reload(){
if (clips > 0) {
animation.Play("reload");
yield WaitForSeconds(1.25);
bulletsLeft = 30;
bulletsPerClip = 30;
clips--;
}
}