The question is answered, right answer was accepted
increase speed of a character costantly
i need to make a movement system in a 2D game where character moves in a direction when you press a button (like some kind of start), and when he starts moving the speed increase costantly, something like Jetpack Joyride or Temple Run for example.
how can i do it?
void Update(){
speed++;
}
:) show us your code and we might be able to give a more specific answer
well, in the meantime i've resolved the problem, but a new one appeared. the speed increase to a certain point, but the AddForce system continues to add force, so it becomes too fast. here's the code:
#pragma strict
var partito=0;
var personaggio:Rigidbody2D;
var accelerazione: int;
var velocita:float;
var jumpForce:int;
var grounded:boolean;
var salto:AudioClip;
var anim:Animator;
function Start () {
anim=GetComponent.<Animator>();
personaggio=GetComponent.<Rigidbody2D>();
}
function Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && partito==0){
velocita=5;
partito=1;
anim.SetTrigger("muovi");
}
if(partito==1 && velocita<6){
personaggio.AddForce(transform.right.normalized*velocita);
velocita+=1*(Time.deltaTime/accelerazione);
}
else if (partito==1 && velocita>=6){
personaggio.AddForce(transform.right.normalized*velocita);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && grounded && partito==1){
anim.SetBool("salto",true);
personaggio.AddForce(Vector2(0,jumpForce*personaggio.mass),Force$$anonymous$$ode2D.Impulse);
GetComponent.<AudioSource>().PlayOneShot(salto,1);
grounded=false;
}
}
function OnCollisionEnter2D(c:Collision2D){
if(c.gameObject.tag=="terreno"){
anim.SetBool("salto",false);
grounded=true;
}
}
if i stop giving addForce aniway, it just slows down and stop, how can i resolve it?
Answer by Shamlei · Jan 19, 2016 at 08:45 PM
Hey :) ! Maybe you could increment your speed variable at the end of each frame by the Update() method.
Like this :
Update()
{
// movement code using speed variable
speed += speedIncrement;
}
I can't really understand your code because i don't speak your language so i don't know if that's what youre doing.
#pragma strict
var started=0;
var character:Rigidbody2D;
var acceleration: int;
var speed:float;
var jumpForce:int;
var grounded:boolean;
var salto:AudioClip;
var anim:Animator;
function Start () {
anim=GetComponent.<Animator>();
character=GetComponent.<Rigidbody2D>();
}
function Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && started==0){
speed=5;
started=1;
anim.SetTrigger("muovi");
}
if(started==1 && speed<6){
character.AddForce(transform.right.normalized*speed);
speed+=1*(Time.deltaTime/acceleration);
}
else if (started==1 && speed>=6){
character.AddForce(transform.right.normalized*speed);
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && grounded && partito==1){
anim.SetBool("salto",true);
character.AddForce(Vector2(0,jumpForce*character.mass),Force$$anonymous$$ode2D.Impulse);
GetComponent.<AudioSource>().PlayOneShot(salto,1);
grounded=false;
}
}
function OnCollisionEnter2D(c:Collision2D){
if(c.gameObject.tag=="terreno"){
anim.SetBool("salto",false);
grounded=true;
}
}
here's a version with translated code parts.