Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with c#. Increase speed of the ball with time

Discussion in 'Scripting' started by jorgegb1997, Dec 12, 2015.

  1. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56
    Hello!


    I have a 3D simple game. It´s just a player that change their position by tapping in the screen of the Android phone.

    Tha player it has a certain speed, but i want that the speed of the player increase with the time (slowly) and stop increasing when reach a certain limit of speed (just like speed = 7).

    Heres the code of the Player:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //
    5. // This script controls the player
    6. //
    7.  
    8. public class playerController : MonoBehaviour {
    9.  
    10.     // minimal physics and collision detection
    11.     // for the plaxer object
    12.     Rigidbody rigidBody;
    13.     // player can roll in two directions
    14.     bool rollingLeft;
    15.     // player speed.
    16.     // faster means more difficult
    17.     public float speed= 4;
    18.  
    19.     // Reference to the main gaming controller script
    20.     gameScript gameScriptReference;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         // get the rigidbody reference
    25.         rigidBody = GetComponent<Rigidbody> ();
    26.         // set starting direction to left
    27.         rollingLeft = true;
    28.         // get reference to the main controller script
    29.         gameScriptReference = GameObject.Find ("GameController").GetComponent<gameScript> ();
    30.     }
    31.  
    32.  
    33.     // Update is called once per frame
    34.     void Update () {
    35.         // only update the player if game is in game mode
    36.         if (gameScriptReference.inGame()) {
    37.             // if player presses space key or
    38.             // taps on the screen,
    39.             // change the direction
    40.             if (Input.GetKeyDown (KeyCode.Space) ||
    41.                 Input.GetMouseButtonDown(0)) {
    42.                 gameScriptReference.addScore(1);
    43.                 rollingLeft = !rollingLeft;
    44.             }
    45.             // player is falling down
    46.             // that means, the game is over
    47.             if (transform.position.y < -10) {
    48.                 gameScriptReference.gameOver();
    49.                 Destroy(gameObject);
    50.             }
    51.         }
    52.     }
    53.  
    54.  
    55.     void FixedUpdate() {
    56.         // only update the player if game is in game mode
    57.         // ball physics
    58.         if (gameScriptReference.inGame()) {
    59.             if (rollingLeft)
    60.                 rigidBody.velocity = new Vector3 (-speed, Physics.gravity.y, 0);
    61.             else
    62.                 rigidBody.velocity = new Vector3 (0, Physics.gravity.y, speed);
    63.         }
    64.     }
    65. }
    66.  
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Simply increment the speed variable either in your Update() method, or make a coroutine if you wish.

    Code (csharp):
    1.  
    2. //maybe as editor settings
    3. public float speedIncrement = 0.01f;
    4. public float maximumSpeed = 7f;
    5.  
    6. //in Update()
    7. speed += speedIncrement;
    8. if(speed >= maximumSpeed) speed = maximumSpeed;
    9.  
     
    Lethn likes this.
  3. jorgegb1997

    jorgegb1997

    Joined:
    Apr 15, 2014
    Posts:
    56

    Thanks! It works!

    Have a good day :)
     
  4. CriticalCoders

    CriticalCoders

    Joined:
    Jun 19, 2021
    Posts:
    2
    But it says name space definition or end-of-file needed
     
  5. JerMurder

    JerMurder

    Joined:
    Jan 5, 2021
    Posts:
    70
    delete 1 } and it will be fine