- Home /
Is it a bad practice to change the timescale for the whole game?
I have a player that shoots out projectiles (arrows). Since they do not move instantaneously, I transform.translate to move them. However, they travel very quickly and "skip" along the screen with each frame. The problem is collisions. If the arrow is too fast, it may skip through an enemy. (The game is 2D by the way.) The arrow can also pierce for an X amount of times. If I want it to pierce for example 1 time before destroying itself, but there are four enemies in the same spot, all collision events happen simultaneously and it pierces 3 times instead of 1. I've tried to illustrate as best as I can here: I've tried different kind of movements for the arrow, but so far I didn't find out anything worthwhile. The only thing I can think of is to raise the Timescale and lower variables such as movement speed, animations speed, etc. My only concerns are if this will change time.deltaTime and if it is a good practice whatsoever.
My question is about changing the Timescale, but if you have any suggestions about how to better handle the arrow movement, I'm open.
Answer by Casiell · Oct 04, 2018 at 06:44 AM
I don't think this is a good idea. You will have a lot of work changing it and remembering about it every time you add something new. Eventually it will have consequences that you won't really anticipate and you will have a lot of problems finding out why something happens. It's not necessarily a bad thing, but for a standard operation like shooting arrow it's certainly an overkill.
You could share the whole portion of your code that moves the arrow, it will be easier to "fix" it this way.
Quick tip: there is Time.unscaledDeltaTime if you really need to change timeScale
void Update () {
this.transform.Translate(new Vector2(1,0) * arrowSpeed * Time.deltaTime);
}
Is the code. I've set the rotation of the arrow in another segment of code, so all I need is the arrow to move forward smoothly. I also considered raycasting, but to my knowledge that happens instantaneously and doesn't go well with a slow-moving arrow.
You should use FixedUpdate for moving objects. Also if you have RigidBody you should probably use Rigidbody.$$anonymous$$ovePosition. It calculates collisions at the same time as movement (not exactly, but close enough) so detection should be better. Also your RigidBody should have collision detection on Continous for moving objects
Also small tip: use Vector2.Right ins$$anonymous$$d of new Vector2(1,0), it doesn't create new objects every frame so it's friendlier for GC, also more readable.
I will use all your tips from now on. Thank you very much for the suggestions. First I tried just setting the collision detection to continuous. The problem persisted. Then I changed the code to Rigidbody.$$anonymous$$ovePosition, however the arrow stopped moving forward completely. The arrow gameObject has a Rigidbody 2d component, that is $$anonymous$$inematic and Simulated. I haven't locked any of the axis.
Your answer
Follow this Question
Related Questions
trying to regulate character speed. 1 Answer
How to detect collision when Time.timeScale == 0? 2 Answers
How do I make colliders collide when Tİme Scale is zero? 1 Answer
Physics Freakouts during Frame Rate Drop ? 1 Answer
Player object falling through game environment, collision not detected with ground 0 Answers