Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 21, 2016 at 04:18 PM by Davide-Barbieri for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Davide-Barbieri · Jan 18, 2016 at 05:38 PM · movement2d-platformerspeed

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Jan 18, 2016 at 06:00 PM 0
Share
 void Update(){
     speed++;
 }

:) show us your code and we might be able to give a more specific answer

avatar image Davide-Barbieri · Jan 19, 2016 at 08:08 PM 0
Share

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?

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Davide-Barbieri · Jan 19, 2016 at 08:52 PM 0
Share
  #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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I want to make a PONG AI that can be beatable. but if i make a AI then either it will be unbeatable or ERROR. please help!!! 1 Answer

How to spawn prefabs with the same speed as the prefabs spawned before? 0 Answers

2D Charaecter Flying Away 0 Answers

Stop moving gameObject and push it back 0 Answers

How to get rid of double movement speed? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges