Hello, everyone. I am working on bullet marks for my gun script. I am, until I make my own, borrowing the bootcamp demo textures. But, the bullet mark’s texture is all wrong. It is offset by about half of the plane.
![alt text][1]
here is the function that calculates the bullet marks:
function PlaceBulletMark(hit : RaycastHit)
{
if(hit.collider.tag == "Wood" || "Metal" || "Concrete" || "Generic")
{
var delta : float = -0.02;
var hitUpDir : Vector3 = hit.normal;
var hitPoint : Vector3 = hit.point + hit.normal * delta;
var hitRotation : Quaternion = Quaternion.FromToRotation(Vector3.forward, hitUpDir);
var obj : GameObject;
var m : Mesh = new Mesh();
var i : float = bmSize/10;
m.vertices = [Vector3(-i, -i, 0.1), Vector3(i, -i, 0.1), Vector3(i, i, 0.1), Vector3(-i, i, 0.1) ];
m.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2(1, 0), Vector2 (1, 1)];
m.triangles = [0, 1, 2, 0, 2, 3];
m.RecalculateNormals();
if(hit.collider.tag == "Wood")
{
obj = new GameObject("Bullet_Mark", MeshRenderer, MeshFilter);
obj.transform.position = hitPoint + (Vector3.up/50);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = WoodBmMaterials[Random.Range(0, WoodBmMaterials.length)];
Debug.Log("Wood");
}
if(hit.collider.tag == "Metal")
{
obj = new GameObject("Bullet_Mark", MeshRenderer, MeshFilter);
obj.transform.position = hitPoint + (Vector3.up/50);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = MetalBmMaterials[Random.Range(0, MetalBmMaterials.length)];
Debug.Log("Metal");
}
if(hit.collider.tag == "Concrete")
{
obj = new GameObject("Bullet_Mark", MeshRenderer, MeshFilter);
obj.transform.position = hitPoint + (Vector3.up/50);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = ConcreteBmMaterials[Random.Range(0, ConcreteBmMaterials.length)];
Debug.Log("Concrete");
}
if(hit.collider.tag == "Generic")
{
obj = new GameObject("Bullet_Mark", MeshRenderer, MeshFilter);
obj.transform.position = hitPoint; //+ Vector3(0, 0.1, 0);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = ConcreteBmMaterials[Random.Range(0, ConcreteBmMaterials.length)];
Debug.Log("Generic");
}
}
}
function GenerateShotGraphics()
{
for(var i = 0; i < ShotEmitter.length; i++)
{
ShotEmitter*.Emit();*
- }*
- if(shotLight != null)*
- {*
-
var go : GameObject = Instantiate(shotLight, tipOfGun.transform.position, Quaternion.identity);*
-
Destroy(go, 0.1);*
- }*
}
and finally, here is the full code. Sorry about the length:
@script AddComponentMenu(“FPS / Weapons / Gun”)
@script RequireComponent (AudioSource)
enum FireType
{ - RayCast,*
- Projectile*
}
enum ShotType
{ - Semi_Auto,*
- Full_Auto,*
}
enum ry
{ - left,*
- forward,*
- right,*
- back*
}
var gunName : String;
var gunRange : float;
var fireRate : float;
private var shootingDelay : float;
var Damage : int;
var fireType : FireType;
var shotType : ShotType;
var tipOfGun : Transform;
private var centerScreen : Transform;
var keyToActivate : KeyCode;
var WoodBmMaterials : Material[];
var MetalBmMaterials : Material[];
var ConcreteBmMaterials : Material[];
var GenericBmMaterials : Material[];
var leaveBM : boolean = false;
var bmSize : float;
var pushPower : float;
var Icon : Texture2D;
var layers : LayerMask;
var Clips : int;
var clipSize : int;
var unlimited : boolean = false;
var autoReload : boolean = true;
var projectilePrefab : Rigidbody;
var projectileShotPower : float;
var reloadElapse : float;
var shotSound : AudioClip;
var OutOfAmmoSound : AudioClip;
var reloadSound : AudioClip;
private var Audio : AudioSource;
var ShotEmitter : ParticleEmitter[];
class p
{
- var useTags : boolean = false;*
- var WoodParticle : GameObject;*
- var MetalParticle : GameObject;*
- var ConcreteParticle : GameObject;*
- var DirtParticle : GameObject;*
- var RockParticle : GameObject;*
- var SnowParticle : GameObject;*
- var WaterParticle : GameObject;*
- var EnemyParticle : GameObject;*
- var UntaggedParticle : GameObject;*
}
var Particles : p = p();
var HParticles : GameObject[];
var shotLight : GameObject;
var gunAnimation : GunAnimation;
var doesAnimate : boolean = false;
@HideInInspector
var shotsLeft : int;
private var freeToShoot : boolean = true;
private var freeToReload : boolean = true;
private var clipsLeft : int;
private var reloading : boolean = false;
private var lastShotTime : float;
private var rlInt : float;
private var v3 : Vector3 = Vector3.zero;
private var rotateRaycast : ry;
function Awake()
{
-
if(!centerScreen)*
-
centerScreen = camera.main.transform;*
-
clipsLeft = Clips;*
-
SetUpRotateRaycast();*
*} *
function OnEnable()
{ -
for(var i = 0; i < ShotEmitter.length; i++)*
-
{*
_ ShotEmitter*.emit = false;_
_ }_
_ Audio = gameObject.GetComponent(AudioSource);_
_ if(shotsLeft < clipSize)_
_ {_
_ clipsLeft–;_
_ shotsLeft = clipSize;_
_ }_
_ if(projectilePrefab)_
_ {_
_ fireType = FireType.Projectile;_
_ }*_
* shootingDelay = 1.0 / fireRate;*
}
function SetUpRotateRaycast()
{
* switch(transform.rotation.y)*
* {*
* case 0:*
* rotateRaycast = ry.forward;*
* break;*
* case 90:*
* rotateRaycast = ry.left;*
* break;*
* case 180:*
* rotateRaycast = ry.back;*
* break;*
* case -90:*
* rotateRaycast = ry.right;*
* break;*
* }*
*} *
function Update ()
{
* HandleInput();*
* HandleValues();*
* HandleReloading();*
}
function HandleInput()
{
* if(Input.GetButton(“Fire1”) && shotType == ShotType.Full_Auto && fireType == FireType.RayCast && Time.time > lastShotTime)
_ {_
_ lastShotTime = Time.time + shootingDelay;_
_ Shoot(false, false);_
} else if(Input.GetButtonDown(“Fire1”) && shotType == ShotType.Semi_Auto && fireType == FireType.RayCast && Time.time > lastShotTime)
_ {_
_ lastShotTime = Time.time + shootingDelay;_
_ Shoot(true, false);_
} else if(Input.GetButtonDown(“Fire1”) && shotType == ShotType.Semi_Auto && fireType == FireType.Projectile && Time.time > lastShotTime)
_ {_
_ lastShotTime = Time.time + shootingDelay;_
_ Shoot(false, true);_
} else if(Input.GetButton(“Fire1”) && shotType == ShotType.Full_Auto && fireType == FireType.Projectile && Time.time > lastShotTime)
_ {_
_ lastShotTime = Time.time + shootingDelay;_
_ Shoot(true, true);_
_ } else {_
_ freeToShoot = true;_
_ }_
_}_
function Shoot(oneShot : boolean, isProjectile : boolean)
_{_
_ if(freeToShoot)_
_ {_
_ var hit : RaycastHit;_
_ var fwd = transform.TransformDirection(v3);_
_ PlayShotSound();_
_ if(!unlimited)_
_ {_
_ shotsLeft–;_
_ }_
_ if(!isProjectile)_
_ { _
_ GenerateShotGraphics();_
_ Debug.DrawRay(centerScreen.transform.position, fwdgunRange, Color.blue);
* if(Physics.Raycast(centerScreen.transform.position, fwd, hit, gunRange, layers)) *
* {*
* GenerateHitGraphics(hit);*
* ApplyForce(hit);*
* }*
* if(oneShot)*
* {*
* freeToShoot = false;*
* }*
* } else*
* {*
* var instantiatedProjectile : Rigidbody;*
instantiatedProjectile = Instantiate(projectilePrefab, centerScreen.position+(transform.forward5), transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3.forward * projectileShotPower);_
if(shotType == ShotType.Semi_Auto)
_ {_
_ freeToShoot = false;_
_ }_
_ }_
_ }_
_}_
function GenerateHitGraphics(hit : RaycastHit) {
_ var go : GameObject;_
_ var delta : float = -0.02;_
_ var hitUpDir : Vector3 = hit.normal;_
_ var hitPoint : Vector3 = hit.point + hit.normal * delta;_
_ var hitRotation : Quaternion = Quaternion.FromToRotation(Vector3.forward, hitUpDir);_
_ if(leaveBM)_
_ PlaceBulletMark(hit);_
_ if(hit.collider.tag != “Player”)_
_ hit.collider.SendMessageUpwards(“ApplyDamage”, Damage, SendMessageOptions.DontRequireReceiver);_
_ if(Particles.useTags)_
_ {_
_ if(!hit.collider.isTrigger)_
_ {_
_ switch(hit.collider.tag)_
_ {_
_ case “Wood”:_
_ go = Instantiate(Particles.WoodParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Metal”:_
_ go = Instantiate(Particles.MetalParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Snow”:_
_ go = Instantiate(Particles.SnowParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Concrete”:_
_ go = Instantiate(Particles.ConcreteParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Dirt”:_
_ go = Instantiate(Particles.DirtParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Rock”:_
_ go = Instantiate(Particles.RockParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Sand”:_
_ go = Instantiate(Particles.DirtParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “water”:_
_ go = Instantiate(Particles.WaterParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Enemy”:_
_ go = Instantiate(Particles.EnemyParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ case “Untagged”:_
_ go = Instantiate(Particles.UntaggedParticle, hitPoint, hitRotation) as GameObject;_
_ break;_
_ }_
_ }_
_ Destroy(go, go.gameObject.GetComponent(ParticleEmitter).maxEnergy);_
_ } else {_
_ for(i = 0; i < HParticles.length; i++)_
_ {_
_ var Go : GameObject = Instantiate(HParticles, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
Destroy(Go, Go.GetComponent(ParticleEmitter).maxEnergy);
}
}
}
function PlaceBulletMark(hit : RaycastHit)
{
if(hit.collider.tag == “Wood” || “Metal” || “Concrete” || “Generic”)
{
var delta : float = -0.02;
var hitUpDir : Vector3 = hit.normal;
var hitPoint : Vector3 = hit.point + hit.normal * delta;
var hitRotation : Quaternion = Quaternion.FromToRotation(Vector3.forward, hitUpDir);
var obj : GameObject;
var m : Mesh = new Mesh();
var i : float = bmSize/10;
m.vertices = [Vector3(-i, -i, 0.1), Vector3(i, -i, 0.1), Vector3(i, i, 0.1), Vector3(-i, i, 0.1) ];
m.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2(1, 0), Vector2 (1, 1)];
m.triangles = [0, 1, 2, 0, 2, 3];
m.RecalculateNormals();
if(hit.collider.tag == “Wood”)
{_
obj = new GameObject(“Bullet_Mark”, MeshRenderer, MeshFilter);
_ obj.transform.position = hitPoint + (Vector3.up/50);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = WoodBmMaterials[Random.Range(0, WoodBmMaterials.length)];
Debug.Log(“Wood”);
}
if(hit.collider.tag == “Metal”)
{_
obj = new GameObject(“Bullet_Mark”, MeshRenderer, MeshFilter);
_ obj.transform.position = hitPoint + (Vector3.up/50);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = MetalBmMaterials[Random.Range(0, MetalBmMaterials.length)];
Debug.Log(“Metal”);
}
if(hit.collider.tag == “Concrete”)
{_
obj = new GameObject(“Bullet_Mark”, MeshRenderer, MeshFilter);
_ obj.transform.position = hitPoint + (Vector3.up/50);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = ConcreteBmMaterials[Random.Range(0, ConcreteBmMaterials.length)];
Debug.Log(“Concrete”);
}
if(hit.collider.tag == “Generic”)
{_
obj = new GameObject(“Bullet_Mark”, MeshRenderer, MeshFilter);
_ obj.transform.position = hitPoint; //+ Vector3(0, 0.1, 0);
obj.transform.rotation = hitRotation;
obj.GetComponent(MeshFilter).mesh = m;
obj.renderer.material = ConcreteBmMaterials[Random.Range(0, ConcreteBmMaterials.length)];
Debug.Log(“Generic”);
}
}
}
function GenerateShotGraphics()
{
for(var i = 0; i < ShotEmitter.length; i++)
{
ShotEmitter.Emit();
}
if(shotLight != null)
{
var go : GameObject = Instantiate(shotLight, tipOfGun.transform.position, Quaternion.identity);
Destroy(go, 0.1);
}
}
function ApplyForce(hit : RaycastHit)
{
var direction : Vector3 = hit.collider.transform.position - centerScreen.position;
var body : Rigidbody = hit.collider.rigidbody;
if(body)
if(!body.isKinematic)
body.AddForceAtPosition(direction.normalized * pushPower, hit.point, ForceMode.Impulse);
}
function HandleValues()
{
if(shotsLeft <= 0)
{
shotsLeft = 0;
freeToShoot = false;
}
switch(rotateRaycast)
{
case ry.left:
v3 = Vector3.left;
break;
case ry.forward:
v3 = Vector3.forward;
break;
case ry.right:
v3 = Vector3.right;
break;
case ry.back:
v3 = -Vector3.forward;
break;
}
}
function HandleReloading()
{
if(Input.GetButtonDown(“Fire2”) || Input.GetKeyDown(KeyCode.R))
{
Reload();
}
if(reloading)
{
rlInt -= Time.deltaTime;
if(rlInt <= 0.0)
{
reloading = false;
shotsLeft = clipSize;
freeToReload = true;
}
}
if(shotsLeft <= 0 && autoReload)
{
Reload();
freeToShoot = false;
}
if(clipsLeft == 0)
{
freeToReload = false;
}
if(reloading)
{
freeToShoot = false;
freeToReload = false;
} else {
freeToShoot = true;
freeToReload = true;
}
}
function Reload()
{
if(freeToReload)
{
if(clipsLeft > 0 && shotsLeft < clipSize)
{
PlayReloadSound();
reloading = true;
rlInt = reloadElapse;
}
if(!unlimited)
{
clipsLeft–;
}
}
}
function PlayShotSound()
{
Audio.PlayOneShot(shotSound);
}
function PlayOutOfAmmoSound()
{
Audio.PlayOneShot(OutOfAmmoSound, 1);
}
function PlayReloadSound()
{
Audio.PlayOneShot(reloadSound);
}
function GetShotsLeft()
{
return(shotsLeft);
}
function GetClipsLeft()
{
return(clipsLeft);
}
function GetName()
{
return(gunName);
}
function Shooting() : boolean
{
if(Input.GetButton(“Fire1”) && shotsLeft > 0)
return true;
else*
* return false;
}
function Reloading() : boolean
{
if(reloading)
return true;
else*
* return false;
}
[1]: Free Image Hosting at FreeImageHosting.net - Upload & Share Images on Social Networks, Blogs, and Galleries