- Home /
2D sprite movement jagger
I've made a javascript which makes it so that the 2D sprite moves side to side randomly for 4-6 seconds. However, it starts to stutter for a while and then proceed to move normally again. I'm pretty sure the problem is that the function starts every tick but I dont know how to fix it.
Here is the code:
function strafe(){
if(right == true){
if(strafeShoot == true){
strafeShoot = false;
rng = Random.Range(4,6);
}else{
transform.Translate(Vector3.right*5*Time.deltaTime);
yield WaitForSeconds (rng);
strafeShoot=true;
right = false;
}
}else{
if(strafeShoot == true){
strafeShoot = false;
rng = Random.Range(4,6);
}else{
transform.Translate(-Vector3.right*5*Time.deltaTime);
yield WaitForSeconds (rng);
strafeShoot=true;
right = true;
}
}
}
https://www.youtube.com/watch?v=MYTvifs2xBk&feature=youtu.be
Here is a link to the problem I'm having, oh btw dont listen to the video, i was listening to a podcast during the recording so it recorded that as well :/
Answer by PythagoreanWarrior · Sep 01, 2015 at 03:36 PM
There is a tool that may help solve the problem in another way. It's InvokeRepeating(function name,start time,interval between function calls) I'll write a code and I hope it satisfies the basic purpose you need and you can build on it:
function Start(){
InvokeRepeating("move",insert start time here,Random.Range(4,6));
}
function move(){
var hey=Random.value;
if(hey>0.5){
//add script for left
}else{
//add script for right
}
}
I hope this function can serve as a basis for your right-left randomiser. Good luck :)
Your answer
Follow this Question
Related Questions
How to forbid an NPC to move diagonally using Character Controller? 1 Answer
Game NPC "Guide" Movement (Much like Navi from Zelda) 1 Answer
Help with 2D AI scripting 2 Answers
AI walk through solid walls 1 Answer
Use of an AI script, and having it NOT go through walls using a character controller 0 Answers