See Below
See belowā¦
Okay sorry to be a blowass, but this might be quite useful out there now that Iāve got it figured out. This script can replace your fps machine gun script, and if you tag your scene objects appropriately, you can have different particle effects for different surfaces that your ābulletsā hit⦠You set up Tags to make this work. The top three inspector slots can be filled by using a default particle systems from the gameobject menu. Enjoy!
var SparkEmitter: GameObject;
var BloodEmitter: GameObject;
var DirtEmitter: GameObject;
var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;
private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
function Start()
{
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, direction, hit, range)){
print ("I'm looking at " + hit.transform.name);
} else {
print ("I'm looking at nothing!");
}
DirtParticles = DirtEmitter.GetComponent(ParticleEmitter);
BloodParticles = BloodEmitter.GetComponent(ParticleEmitter);
SparkParticles = SparkEmitter.GetComponent(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (DirtParticles)
DirtParticles.emit = false;
// We don't want to emit particles all the time, only when we hit something.
if (BloodParticles)
BloodParticles.emit = false;
// We don't want to emit particles all the time, only when we hit something.
if (SparkParticles)
SparkParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate()
{
if (muzzleFlash)
{
// We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount)
{
muzzleFlash.transform.localRotation = Quaternion.AxisAngle(Vector3.forward, Random.value);
muzzleFlash.enabled = true;
if (audio)
{
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
}
// We didn't, disable the muzzle flash
else
{
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire ()
{
if (bulletsLeft == 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 (){
DirtParticles = DirtEmitter.GetComponent(ParticleEmitter);
BloodParticles = BloodEmitter.GetComponent(ParticleEmitter);
SparkParticles = SparkEmitter.GetComponent(ParticleEmitter);
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 (DirtParticles&hit.collider.transform.CompareTag("Ground"))
{
DirtParticles.transform.position = hit.point;
DirtParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
DirtParticles.Emit();
}
if (BloodParticles&hit.collider.transform.CompareTag("Enemy"))
{
BloodParticles.transform.position = hit.point;
BloodParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
BloodParticles.Emit();
}
if (SparkParticles&hit.collider.transform.CompareTag("Metal"))
{
SparkParticles.transform.position = hit.point;
SparkParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
SparkParticles.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 () {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () {
return bulletsLeft;
}
targos: great script!
can you help me with a noob question? do I attach the script to the GameObject with the Camera?
Hiya!
If you open the FPS example project, you will see where the machinegun.js script is located in the FPS/PLAYER controller hieracy. you can swap that one for mine, but you will additionally have to change some scene objects:
Define The Tags in the Machinegun1.js script.
Tag scene objects appropriately in the inspector.
Heres an example. Remember to have particle systems as children of the player(or somewhere in the scene. The rocketlauncher script is purposely not there, and will throw an error, but thats pretty self explanatory.
Have fun, let me know if it doesnt work.
AC
53950ā1961ā$machinegunadvancedunitypackage_317.zip (466 KB)
thanks for the reply. was getting ahead of myself. the FPS tutorial is a good idea. same advice from our unity programmer at work.
you are a great source of knowledge and most willing to share. itās easy to see why you are so popular in the forums.
-fawning sycophant
Thanks for the compliments, though Im still not sure what fawning sycophant means. This is the first online forum Iāve ever been involved in, and Iāve learned that if you piss people off, they tend to stay pissed off for a really long time.
Thats life huh, I shouldve hung round on the Torque forum while I learned some mannersā¦
Its cool that you have a Unity programmer at work. I wish big studios would adopt Unity so my skillset is more employable. The better I get with Unity, the more niche my skillset gets.
So yeah if anyones got any paying work?
Untill then Iāll just keep working on my game until its playable/sellable
Itās a massive job finishing a solo project!
Cheers
AC
we are working at a university making simulations. currently, we are creating a beermaking sim and have started modeling the brewery on campus.
Right now, there is no real money, but if the prototype is good enough, it may attract backing from large beer company or grant money.
idea is to work through steps in beer-making at a mid-scale brewery, like levels in a game. then get master brewer status and enter different challenges, like: brew a specific type of beer by choosing methods and ingredients, increase the capacity of brewery, or some kind of interactive speadsheet type sim where you have to keep a competetive brewery in business.
itās a big idea, for now, weād just like to make an immersive environment and prototype some exercises or challenges in the beer-making process.
an example of an exercise might be a demonstration of a hydrometer with some quiz material attached.
Particles are not showing for me
Hi Matt,
Click the www button below my post and it will take you to my website. Click the stuff tab, and theres and example unityPackage.
Have fun
AaronC
www