Why is this small part of code not frame independent ?
I have this code and been messing around with time.deltatime for quite a long time now, not sure why it isnt frame independant, please help.
void Update()
{
Vector3 velocity = bulletSpeed * transform.forward + gravity;
//Debug.Log (transform.position);
//Debug.Log (velocity);
gravity += new Vector3 (0, -9.8f, 0);
newPos = velocity * Time.deltaTime + transform.position;
direction = newPos - oldPos;
float dist = direction.magnitude;
if(Physics.Raycast(oldPos,direction,out hit,dist,layersToCheck))
{
//Debug.Log("hit Object name: " + hit.collider.gameObject.name);
Destroy(gameObject);
}
else
{
oldPos = transform.position;
transform.position = newPos;
}
Answer by wibble82 · Sep 06, 2015 at 10:51 AM
Changing answer after I was corrected.
On closer inspection, this approach is a little curious. I'm guessing what you want is a bullet that is fired outwards in the direction of 'transform.forward' then moves in a curve due to gravity.
However its a little strange for a few reasons. It is kind of half way between doing things parametrically (recalculating the position every frame based off initial position/velocity) and iteratively (tracking velocity/position and incrementing every frame.
You seem to be building up velocity-due-to-gravity every frame, then adding this on to a fixed velocity value.
I would approach it this way:
Vector3 velocity;
Vector3 gravity = new Vector3(0,-9.8,0);
void Start()
{
velocity = bulletSpeed * transform.forward;
}
void Update()
{
velocity += gravity * Time.deltaTime;
Vector3 new_pos = transform.position + velocity * Time.deltaTime;
}
the line 9 is supposed to add gravity over each frame, so that it creates a curved trajectory rather than a straight falling line. Edit: as in gravity adds up each frame to become bigger, as the object travels forward, this causes the object to fall in a curve
Hey there.
Yes I just noticed you redefine velocity every frame. This is a slightly odd way to write it. Typically gravity is a fixed acceleration, and you increase velocity every frame.
I guess what you're effectively doing here is storing gravity as a velocity. It is really 'how much velocity I have gained so far due to gravity'.
Either is valid - your is just a little less traditional :) In that case, you'll want to scale that gravity increment by Time.deltaTime each frame. I'll modify my answer above to update it.
yes that works now, but the thing is, it has to be a really low bullet speed otherwise its way too fast in forward direction and barely falls due to gravity, where as when bullet speed is low , you can see the gravity take effect, but its really really slow forward movement
Well, turn up the gravity then :) You can make it do whatever you like just by playing with the numbers. What that code gives you is simply the curve that a body will undergo due to gravity. If you use -100 ins$$anonymous$$d of -9.8, it'll fall faster.
But because im using real scale models ( 1um = 1m ) , shouldnt there be a way it works properly using real life values, bullet speed is 1500 velocity, while gravity is -9.8 down acceleration. if i use the above code that i posted ( frame dependant one ) it works pretty good ( compared to the other trajectory code i wrote using trajectory equations for non horizontal launched projectiles). But ill probably change the values for gravity and see how it goes.
Your answer
Follow this Question
Related Questions
Load scene after time 1 Answer
Having trouble increasing the spawning speed of explosions 0 Answers
Farming game Time management 0 Answers
movement speed still changes with FPS; Despite using Time.deltaTime (And GetAxisRaw)! 0 Answers
How is it possible to verify values and is they are correct activate objects ? 0 Answers