- Home /
 
               Question by 
               psyence88 · Mar 03, 2015 at 12:10 PM · 
                raycasttransformpositionmachinegun  
              
 
              parented/constrained object transform lags when moving character
Hi I have a simple raycast-base gun that emits particles along it's path to fake bullets. Whenever I move the camera/character- the particles are misaligned, as well as anything else I try to modify its transform with in scripting. I believe the culprit to be slerp but I am not sure how else I can write it. I've attached the script for any details. Any ideas would be awesome, this issue has been plaguing me all day.
using UnityEngine; using System.Collections;
 public class raycast : MonoBehaviour {
     private Transform mainTransform;
     public GameObject mesh_input;
     public GameObject path_obj;
     public float spread;
     public float range;
     //right now rate and pathtime are tied together, which means the gun fires again after each successive hit
     //we need to separate these into individual components so we can fire many shots but have them take time
     //to reach their destination
     public float rate = 0.5F;
     private float nextFireTime = 0.0F;
     public int ammo = 600;
     private enemy target;
     private Rigidbody enemycollider;
     public static Transform particles;
     public Transform muzzle;
     public GameObject muzzleflash;
 
     void OnGUI (){
         GUI.Box (new Rect (10, 10, 200, 20), "Weapon: Color Pistol");
         GUI.Box (new Rect (10,40, 200, 20), "Ammo: " + ammo.ToString());
         GUI.Box (new Rect (10,70, 200, 20), "Rate: " +  ((((60 / rate)/60)*100F)/100F) + "bps");
         GUI.Box (new Rect (10,100, 250, 20), "z = decrease rate, x = increase rate ");
     }
 
 
     IEnumerator MoveThis(Transform origin, Vector3 destinationPosition, float period, Transform obj, RaycastHit hit) {
         float t = 0F;
         print ("coroutine: " + origin.transform.position);
         obj.transform.position = origin.transform.position;
         while (t < 1) {
             obj.transform.position = Vector3.Lerp (obj.transform.position, destinationPosition, t);
             t += Time.deltaTime / period;
             yield return null;
         }
         GameObject prefab = Instantiate (mesh_input, destinationPosition, Quaternion.identity) as GameObject;
         float rand = Random.Range (0F,3F);
         prefab.transform.localScale = new Vector3(rand,rand,rand);
         prefab.transform.localPosition = prefab.transform.localPosition + new Vector3 (Random.Range (-1*spread,spread), 0, Random.Range (-1*spread,spread));
         //    print ("Hit!");
         ammo -= 1;
         if (hit.collider.tag == "enemy") {
             target = hit.collider.GetComponent<enemy> ();
             target.addDamage(51);
             enemycollider = hit.collider.GetComponent<Rigidbody>();
             enemycollider.rigidbody.AddForceAtPosition(new Vector3(-50,1,0) * Vector3.Angle(gameObject.transform.position, hit.point), hit.point);
         }
     }
     
     void fire(RaycastHit hit, Transform trans){
 
         GameObject path = Instantiate (path_obj, mainTransform.position, Quaternion.identity) as GameObject;
         Transform particles = path.transform.Find ("mover");
         StartCoroutine (MoveThis(trans,hit.point,rate, particles, hit));
 
     }
 
     void raycaster(bool buttoncheck, Transform trans){
                 RaycastHit hit;
                 Vector3 fwd = mainTransform.TransformDirection (Vector3.up);
                 if (Physics.Raycast (mainTransform.position, fwd, out hit, range) && buttoncheck == true) {
             //            print ("Hit detected");
             //            print (hit.distance.ToString ());
                         fire (hit, trans);    
             Debug.DrawLine (mainTransform.position, hit.point, new Color (0, 0, 1, 1));
                 }
         }
 
 
 
     void Awake(){
     }
 
     // Update is called once per frame
     void Update () {
         mainTransform = muzzle.transform;
         print ("Fixed: " + mainTransform.position.ToString ());
         bool fire1 = Input.GetButton ("Fire1");
         bool fire2 = Input.GetButton ("Fire2");
         if (fire1 == true && ammo != 0 && Time.time > nextFireTime) {
             Instantiate (muzzleflash, muzzle.transform.position, transform.rotation);
             nextFireTime = Time.time + rate;
             raycaster(fire1, mainTransform);                
         }
         if (fire2 == true) {    
             ammo = Mathf.Clamp(600,0,600);
         }
         if (Input.GetKeyDown("z") && rate >= 0){
             rate = Mathf.Clamp (rate + .01F, .01F, .99F);
         }
         if (Input.GetKeyDown ("x") && rate <= 1){
             rate = Mathf.Clamp(rate - .01F, .01F , .99F);
         }
 
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                