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 /
avatar image
0
Question by stephen_george98 · Apr 14, 2016 at 10:58 PM · unity 5timevelocityspeedhealth

How to Get Speed to Very Gradually Pick Up Over Time?

Hello all,

Very simple with what is probably a very simple solution, but I just cannot figure this one out.

My game is an over-the-shoulder endless runner style game. I have the obstacles coming down the screen with this VERY simple script:

 using UnityEngine;
 using System.Collections;
 
 public class ObstacleMover : MonoBehaviour {
 
     private Rigidbody rb; // Reference to Rigidbody Component
     public float speed;
 
     void Start() 
     {
         rb = GetComponent<Rigidbody> ();
         // In Unity 5, you have to now have a reference to a component.
         rb.velocity = -transform.forward * speed;
         /* Transform is negative because in my game,  I have the
            illusion of the objects coming down my mountain, towards the
            player, where the camera is attached as a child.
         */
     }
 }

My question is, how do I make the 'speed' make the obstacle go gradually faster as the game progresses and the player goes longer into the game AND then have the speed top out (or just stop increasing and just stay constant) after a good while of the player surviving? I tried a few ideas with Mathf.Clamp, Time.deltaTime, and Vector3.MoveTowards, but they just did not give me the desired results. Thank you to all who attempt and help me solve this! :)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by mptp · Apr 15, 2016 at 01:39 AM

try something like this:

 private Rigidbody rb;      //Reference to Rigidbody Component
 public float speed;        //Speed, updated through script
 public float acceleration; //Every second, the speed will increase by this much

 //Executes once, when object is spawned / scene loaded
 void Start() {
     //Get reference to rigidbody, and set the speed
     rb = GetComponent<Rigidbody> ();
     rb.velocity = -transform.forward * speed;
 }

 //Executes every frame
 void Update() {
     //Add acceleration to speed, make sure it's not above topSpeed)
     speed += Time.deltaTime * acceleration;

     //Set object velocity
     rb.velocity = -transform.forward * speed;
 }

The important thing is that you're updating the speed value every frame in the Update() function by a new parameter: acceleration.

Comment
Add comment · 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
0

Answer by stephen_george98 · Apr 15, 2016 at 02:00 PM

@mptp Thank you so much! This looks great.

I am getting Error CS0029 cannot implicitly convert type 'float' to 'UnityEngine.Vector3' on the line " rb.velocity = speed; ".

I'm sure it is because .'velocity' is recognized as a vector 3 and Unity cannot equate a float to a vector because a vector consists of 2, 3 or 4 floats. So Unity does not know which one to equate. Am I correct? How could I fix this? Thanks a million

Comment
Add comment · Show 2 · 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 mptp · Apr 15, 2016 at 02:06 PM 0
Share

$$anonymous$$y apologies! I got myself mixed up!

A Vector3 is more than just a list of floats, in unity it's a whole class, but you're right in that you can't use it as a float.

I've updated my answer to treat speed as a float.

P.s. For the future, this should have been posted as a comment to my answer, rather than as a whiwhole new answer.

avatar image stephen_george98 mptp · Apr 15, 2016 at 04:05 PM 0
Share

Oh okay I gotcha now. Thank you so much for your help @mptp . It works just how I need it to. :)

And I'm sorry I made my response an answer ins$$anonymous$$d of a comment. I clicked on update then this big text box came up so I typed in it. I clicked "reply" on this so hopefully this is a comment, not another answer response. Thanks again! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

velocity behaviour - can't make velocity to 0. 0 Answers

What is csc.rsp file and How can I add it to Unity assest? 1 Answer

Bouncy Ball stops without enough force 0 Answers

Vector3 magnitude broken? 1 Answer

Code Help Needed. Anti-Time Cheat System Not Working !!! 0 Answers


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