• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
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

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Place Gameobject within a specific tranform value 3d 3 Answers

calculating my position from another object 1 Answer

How to make a gameobject rotation equal to ray transform? 0 Answers

transform position and rotation of instantiated object 1 Answer

Negative Positions Breaks Raycasts 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges