# Moving object by specific value

**URL:** <https://discussions.unity.com/t/moving-object-by-specific-value/419936>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [August 6, 2010, 4:02pm UTC](https://discussions.unity.com/t/moving-object-by-specific-value/419936 "2010-08-06T16:02:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_28086103](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@anon\_28086103](https://discussions.unity.com/u/anon_28086103)\
**Post date:** [August 6, 2010, 4:02pm UTC](https://discussions.unity.com/t/moving-object-by-specific-value/419936/1 "2010-08-06T16:02:52Z")

</div>

Hello

I have a simple ball. My aim is to make it moving in labirynth using keyboard such that one press of arrow moves ball by one piece of wall:

```auto
xxxx xxxx      
 o => o       
xxxx xxxx

```

I use for that script:

var move = transform.TransformDirection(Vector3.forward);  
var controller : CharacterController = GetComponent(CharacterController);  
var deltaX = transform.position.x;  
controller.SimpleMove(move\*MOVE\_VALUE);  
deltaX = transform.position.x - deltaX;  
print(“Moving along x=” + deltaX);

where MOVE\_VALUE is some constant.

Problem is, that deltaX for every press of button has different value (from about 0,11 to 0,2). Second case is that ‘moving unit’ is _very_ different from ‘size unit’ - experimentally obtained MOVE\_VALUE is about 62 to move ball by 1 ‘size unit’.

How can I solve this problem?

---

<div class="post-metadata">

**Author:** ![anon\_28086103](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@anon\_28086103](https://discussions.unity.com/u/anon_28086103)\
**Post date:** [August 7, 2010, 12:02pm UTC](https://discussions.unity.com/t/moving-object-by-specific-value/419936/2 "2010-08-07T12:02:16Z")

</div>

If I use Move function instead of SimpleMove, then distance is ok, but moving itself is not smooth like in SimpleMove… How can I connect proper distance of Move function with ‘smoothness’ of SimpleMove?  
And in general why with the same argument Move function performes moving object by another distance than SimpleMove?

---

<div class="post-metadata">

**Author:** ![andeeeee](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@andeeeee](https://discussions.unity.com/u/andeeeee)\
**Post date:** [August 9, 2010, 2:54pm UTC](https://discussions.unity.com/t/moving-object-by-specific-value/419936/3 "2010-08-09T14:54:24Z")

</div>

The distance moved by [SimpleMove](http://unity3d.com/support/documentation/ScriptReference/CharacterController.SimpleMove.html) is not guaranteed to be an exact step. You’ll notice that the parameter for this function is actually a speed value, so the distance will depend on the frame time. For something like this, it is better to set the object’s postion directly using [transform.Translate](http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html). You can use [Input.GetKeyDown](http://unity3d.com/support/documentation/ScriptReference/Input.GetKeyDown.html) to detect the first frame where a key is pressed, so the key will not repeat if you hold it down:-

```auto
var stepSize: float; // Distance between wall blocks.

function Update() {
   if (Input.GetKeyDown("d")) {
      transform.Translate(Vector3.right * stepSize;
   }
}

```
