- Home /
Question by
tachikoma · Jun 28, 2011 at 08:09 AM ·
particlesmuzzleflash
muzzle flash and particle emit didn't show when shot
Here is my code, I attached this to the launcher position.And drag the mesh and particle to the slot. But anything happened when shot. Also ,I don't understand " enabled = false" usage in function lateupdate(), may be confused with "muzzleFlash.enabled = false;" but when I deleted "enabled= false",I can see the muzzle flash. But particle still not been seen. Anyone can give help?
var projectile : Rigidbody;
var speed = 150;
var fireRate = 0.05;
var damage = 5.0;
var reloadTime = 5;
var range = 1000;
var bulletsPerClip = 30;
var clips = 20;
var muzzleFlash : Renderer;
var nextFireTime = 0.0;
var m_LastFrameShot = -1;
var hitParticles : ParticleEmitter;
var force = 1;
private var bulletsLeft;
function Start()
{
bulletsLeft = bulletsPerClip;
hitParticles = GetComponentInChildren(ParticleEmitter);
if(hitParticles)
{
hitParticles.emit = false;
}
}
function Update ()
{
if( Input.GetButton("Fire1")&&Time.time > nextFireTime)
{
nextFireTime = Time.time + fireRate;
FireOneShot();
}
}
function FireOneShot()
{
var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2));
var hit : RaycastHit;
var hit2: RaycastHit;
var ourTarget : Collider;
var direction;
if(Physics.Raycast(ray,hit,range))
{
Debug.DrawLine( transform.position, hit.point, Color.red);
Debug.Log(hit.point );
direction = (hit.point - transform.position).normalized;
if (Physics.Raycast(transform.position,direction,hit2,range))
{
if (hit2.collider.tag == "Enemy")
hit2.collider.SendMessage("ApplyDamage",damage);
if(hitParticles)
{
print("hitparticle");
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation =
Quaternion.FromToRotation( Vector3.up, hit.normal);
hitParticles.Emit();
}
}
}
bulletsLeft--;
m_LastFrameShot = Time.frameCount;
enabled = true;
print(bulletsLeft);
if(bulletsLeft ==0)
Reload();
}
function LateUpdate()
{
if(muzzleFlash)
{
if (m_LastFrameShot == Time.frameCount)
{
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
muzzleFlash.enabled = true;
}
else {
muzzleFlash.enabled = false;
enabled = false;
}
}
}
function Reload()
{
yield WaitForSeconds(reloadTime);
if( clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
else
print("You haven't enough ammo");
}
function GetBulletsLeft ()
{
return bulletsLeft;
}
function OnGUI()
{
GUI.Label(new Rect(Screen.width-128-100,228,200,128), "BulletLeft:" + bulletsLeft);
}
Comment
I reformatted your code. Nicely formatted code gets more ANSWERS!
Answer by Alex 4 · Jun 28, 2011 at 06:16 PM
Do you have all variables assigned?
Please don't post other questions or clarifications as an answer; the answer area should be for solutions to avoid clutter.