- Home /
How to move character by command?
I am using Character Controller and i want to know how to move my character using command, for example i want my character to step back (move few units backward), but it should move, not be teleported by instant. I've tried to do this using "for" loop and "waitforseconds" but it's not enough. "waitforseconds" has big limits, so even if i put "0.0000000000001" seconds it doesn't move that fast.
Here is my function:
function jump()
{
var controller : CharacterController = GetComponent(CharacterController);
var i :byte;
for(i=1;i<=60;i++)
{
moveDirection = Vector3(0, 0, -1);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
yield WaitForSeconds(0.000001);
}
}
Answer by JanusMirith · Oct 26, 2014 at 10:37 PM
If you just want to wait for a frame then you need to use:
yield;
But you still have a issue with your logic, here you do something for a set number of frames, this would make the action behave differently at different frame rates.
You would need to use a counter:
float time;
while(time<1f){
time += Time.DeltaTime;
//Do your code here
yield; // wait for a frame
}
Thanks, yeah i know it would, but it's not bad? I mean Update (function where normal moving is made) is updated once per frame too, so lower framerate will make all of them (normal moving and my jump function) just slower so it's okay.
You are going to regret that sooner or latter. It is best to design from the start to be frame rate independent.
Regardless is the yield command what you were looking for?
Why? Function "Update" is updated once per frame, so you say that we shouldn't use this function?
It is updated once per frame, but framerate is hardly consistent. Things will zip along in faster devices and things will crawl along in slower ones. Your movement won't be consistent. So if you have something that is dependent on correct movement, it won't work. That is why that deltatime is used. A projectile for example needs to be frame rate independent.
I don't understand what do you mean, so how my function should look like?
I have this:
function jump()
{
var controller : CharacterController = GetComponent(CharacterController);
var i :byte;
for(i=1;i<=10;i++)
{
moveDirection = Vector3(0, 0, -6);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= 3;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
yield;
}
}
So fix that if you can, to move my character for 60 units backward.
Your answer
Follow this Question
Related Questions
CharacterController.SimpleMove() doesn't exist? 0 Answers
Very Simple Left and Right Movement Script [OR] disable dravity on CharacterController? 2 Answers
Move() function doesn't work on character controller! 0 Answers
Help with CharacterController.Move 0 Answers
Character Flying around uncontrollably 2 Answers