x


Bullet Mark Texturing offset too far

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

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[i].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[i].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, fwd*gunRange, 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.forward*5), 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[i], 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[i].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;
 }


more ▼

asked Jul 01, 2011 at 01:03 PM

zmar0519 gravatar image

zmar0519
1k 165 127 143

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Next time include "relevant" parts only ;)

It looks like you've messed up your uvs ;)

You have:

  vertices        UVs
 -i, -i, 0.1      0, 0
  i, -i, 0.1      0, 1
  i,  i, 0.1      1, 0
 -i,  i, 0.1      1, 1

You should have:

  vertices        UVs
 -i, -i, 0.1      0, 0
  i, -i, 0.1      1, 0
  i,  i, 0.1      1, 1
 -i,  i, 0.1      0, 1

If you swap x and y the texture would be rotated by 90° but the real mistake is that (i, i) have to match (1, 1) in any case ;)

more ▼

answered Jul 01, 2011 at 03:52 PM

Bunny83 gravatar image

Bunny83
77.7k 26 105 335

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3581
x640
x189
x17

asked: Jul 01, 2011 at 01:03 PM

Seen: 1771 times

Last Updated: Jul 01, 2011 at 03:52 PM