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:
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?
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?
The distance moved by SimpleMove 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. You can use Input.GetKeyDown to detect the first frame where a key is pressed, so the key will not repeat if you hold it down:-
var stepSize: float; // Distance between wall blocks.
function Update() {
if (Input.GetKeyDown("d")) {
transform.Translate(Vector3.right * stepSize;
}
}