c# - Object coordinates not updating as expected -


i'm having issue prefab i'm instantiating in script. i'm attempting simulate physics behind projectile motion of cannonball. have following script attached prefab:

using unityengine; using system.collections;  public class shootphysics : monobehaviour {      // vectors & angles calculated     private float fx = 0f;     private float fy = 0f;     private float angle;      // forces acting upon object     private float gravity = 9.8f;     private float force = 1.2f;      // new distance & vector variables     private float dis_x = 0f;     private float dis_y = 0f;      // script & cannonball transform     private cannonphysics cannon_angle;      void start() {         cannon_angle = gameobject.findwithtag("gun").getcomponent<cannonphysics>();         angle = cannon_angle.getangle ();         fx = mathf.round (force * mathf.cos(mathf.deg2rad * angle));         fy = mathf.round (force * mathf.sin(mathf.deg2rad * angle));     }      void fixedupdate() {         dis_x = (float)(fx * time.timesincelevelload);         dis_y = (float)(fy + time.timesincelevelload + (0.5) * (-gravity) * time.timesincelevelload * time.timesincelevelload);         debug.log (dis_x+", "+dis_y);         gameobject.transform.translate (new vector2(dis_x, dis_y));     }  } 

and prefab instantiated following script:

using unityengine; using system.collections;  public class cannonphysics : monobehaviour {      // our script rotate our cannon     private float angle = 0f;      // cannonball object     public transform firepoint;     gameobject cannon = null;      void update() {         if(input.getkey(keycode.uparrow)) {             angle = angle + 1;         }         if(input.getkey(keycode.downarrow)) {             angle = angle - 1;         }          // update cannon angle         transform.rotation = quaternion.euler (new vector3(0, 0, angle));          if(input.getmousebuttondown(0)) {             createcannon();         }     }      public float getangle() {         return angle;     }      private void createcannon() {         cannon = instantiate (resources.load("prefabs/cannonball"), firepoint.position, firepoint.rotation) gameobject;         destroy (cannon, 3f);     }  } 

the object gets instantiated should, , object destroyed after 3 seconds should, object x , y position not change @ all, , not should expect them be. let me give example:

these x , y coordinates should expect have displaying in console.

enter image description here

however these actual coordinates displayed in unity's inspector:

enter image description here

what's strange too, these coordinates not change each object, literally stay same, after object has been destroyed. can explain why happening? , perhaps offer solution?

edit: more clear example:

each time instantiate cannonball object x , y coordinates next object instantiated referenced last. here result of first instantiated object after being destroyed.

enter image description here

and here after instantiating second object.

enter image description here

now i'm expecting each new object instantiated should start "original" position (figuratively speaking) , x , y values should reset , start again effectively.

there few things happening in code have provided in fixedupdate() portion of code have copied below:

void fixedupdate() {     dis_x = (float)(fx * time.timesincelevelload);     dis_y = (float)(fy + time.timesincelevelload + (0.5) * (-gravity) * time.timesincelevelload * time.timesincelevelload);     debug.log (dis_x+", "+dis_y);     gameobject.transform.translate (new vector2(dis_x, dis_y)); } 

first, time.timesincelevelload provides time, in seconds, has passed since level loaded. means code dis_x , dis_y have larger values longer have application open. so, when spawn new prefab, d_x , d_y values @ position seems if it's using position of last prefab.

instead, if want calculate new position of object given it's velocity, you'd typically want use deltatime instead. example:

dx = vel.x * time.fixeddeltatime; dy = vel.y * time.fixeddeltatime; 

in example, dx , dy represent change in position given velocity of object. time.fixeddeltatime represents amount of time in seconds has passed since last time fixedupdate() called. add dx , dy position new position.

secondly, gameobject.transform.translate() translates position of gameobject; not set position of gameobject. translate add vector3 current position. why debugging output looks strange; dis_x , dis_y not positions; how far moved gameobject calling translate().

to set position of gameobject, instead:

gameobject.transform.position = newposition; 

...where newposition vector3 represents new position of gameobject.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -