- Home /
Control the speed of movement of object?
I am using the following code. How to control the speed of movement of object?
Vector3 position = turret2X.transform.localPosition; position.z = Mathf.Clamp(position.z + ivalue, 0.05f, -0.066f); turret2X.transform.localPosition = position;
Answer by Captain_Pineapple · Jul 05, 2020 at 07:37 AM
can you add some more context? Like if ivalue
(which really seems to be a badly chosen variable name as it tells nothing about it's contained value) is already using Time.deltaTime;
which is really important here if the posted code is in your Update()
.
then can you specify what you mean by control? Do you want to make it consistent? Do you want to control it with keys? with mouse? whats the goal here?
ivalue is input given by user that is any number.
By control i mean .. How will i reduce the speed of movement of object...
Okay not sure if you really understand your current code then.
you currently do the following:
take the objects position.
add a userdefined "speed" value on top.
Constrict the resulting position between 0.05 and -0.066. (which in itself is a bit weird...)
so what you should do ins$$anonymous$$d is something like this:
float maximumDistancedTraveledPerSecond = 5f;
position.z += $$anonymous$$athf.Clamp(ivalue * Time.deltaTime, -maximumDistancedTraveledPerSecond, maximumDistancedTraveledPerSecond);
float maximumPositionOfObjectInZ = 10;
position.z = $$anonymous$$athf.Clamp(position.z, -maximumPositionOfObjectInZ, maximumPositionOfObjectInZ);
this would constrain your objects position in z direction between + and - 10 and will constrain the speed of your object to a maximum of 5 units per second.
then once again please be more specific next time. "It does not work" or "how will i reduce the speed" is not really telling me what goes wrong or does not behave as you intend it to be. Details are important here.
Your answer
Follow this Question
Related Questions
Find a gameobject with the same position 2 Answers
Updating localPosition(?) in a scaled transform parent 1 Answer
GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers
Setting position on parent transform with many children - performance 0 Answers
How to set the position of a guitext using transform? 1 Answer