Search Unity

My Ball randomly jumps on my racing course

Discussion in 'Physics' started by Seelka, Dec 13, 2015.

  1. Seelka

    Seelka

    Joined:
    Dec 13, 2015
    Posts:
    2
    Hello Girls & Boys,

    I've got a problem with my current Unity school project.
    My Ball, like mentioned in the title, randomly jumps on my racing track. :eek:

    There is no specific place where he does this, but it's ruining my entire game, pretty sad and annoying. :confused:
    Anyone else had this problem or something like that? If you wanna check some of my PlayerMovement code or something like that, feel free to ask, im willing to post it, if it's necessary.


    Thanks for reading. :D
    mfg Nico
     
  2. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Hi wellcome. I recomend you post your code and you ball Inspector settings.
     
  3. Seelka

    Seelka

    Joined:
    Dec 13, 2015
    Posts:
    2
    Thanks for your quick answer, here are the requested thingys :p
    ss+(2015-12-14+at+12.30.40).png
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public GameObject CamRig;
    8.  
    9.     public float SlowDownMulti = 2;
    10.     public float SpeedUpMulti = 5000;
    11.  
    12.     public float minSpeed = 10;
    13.     public float midSpeed = 45;
    14.     public float maxSpeed = 100;
    15.  
    16.     public float minEvadeSpeed = 40;
    17.     public float midEvadeSpeed = 60;
    18.     public float maxEvadeSpeed = 80;
    19.  
    20.     public float movementSpeed = 10;
    21.     public float EvadeSpeed = 80;
    22.     public float Acceleration = 1;
    23.  
    24.     private float horizontal = 0;
    25.     private float vertical = 0;
    26.  
    27.     private Vector3 RigRelativeForward;
    28.     private float RigRelativeBackwards;
    29.     private float RigRelativeLeft;
    30.     private float RigRelativeRight;
    31.  
    32.     //private Vector3 movement;
    33.  
    34.     private Rigidbody rb;
    35.  
    36.  
    37.     void Start() {
    38.  
    39.         rb = GetComponent<Rigidbody>();
    40.         //rb.maxAngularVelocity = 50;
    41.         //movementSpeed = minSpeed;
    42.  
    43.     }
    44.  
    45.     void LateUpdate() {
    46.  
    47.         velo();
    48.  
    49.     }
    50.  
    51.     void FixedUpdate() {
    52.        /* float moveHorizontal = Input.GetAxis("Horizontal");
    53.         float moveVertical = Input.GetAxis( "Vertical" );
    54.  
    55.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);*/
    56.  
    57.         //rb.AddForce(movement  * speed);
    58.  
    59.         AutoForward();
    60.         ForwardAcceleration();
    61.         SlowDownAcceleration();
    62.         PlayerEvade();
    63.         PlayerRotation();
    64.         evadeSpeedRegulator();
    65.         noBackwardsDriving();
    66.         //rb.AddForce(movement * movementSpeed);
    67.     }
    68.  
    69.  
    70.     void AutoForward () {
    71.  
    72.         Vector3 RigRelativeForward = CamRig.transform.TransformDirection(Vector3.forward) * Time.deltaTime;
    73.         /*vertical = 1 * movementSpeed * Time.deltaTime;
    74.         Vector3 movement = new Vector3(0.0f, 0.0f, vertical);*/
    75.         rb.AddForce(  RigRelativeForward *  movementSpeed * SpeedUpMulti );//movement
    76.  
    77.     }
    78.  
    79.     void ForwardAcceleration() {
    80.  
    81.         if (Input.GetAxis("Vertical") > 0) {
    82.  
    83.             Vector3 RigRelativeForward = CamRig.transform.TransformDirection(Vector3.forward) * Time.deltaTime;
    84.             /*vertical = Input.GetAxisRaw("Vertical") * Time.deltaTime;
    85.             Vector3 movement = new Vector3(0.0f, 0.0f, vertical);*/
    86.             if (movementSpeed < maxSpeed) {
    87.  
    88.                 movementSpeed += Acceleration;
    89.          
    90.             }
    91.             else if (movementSpeed >= maxSpeed)
    92.             {
    93.                 movementSpeed = maxSpeed;
    94.             }
    95.            // rb.AddForce(RigRelativeForward * movementSpeed);//movement
    96.  
    97.         }
    98.  
    99.     }
    100.  
    101.     void SlowDownAcceleration() {
    102.  
    103.         if (Input.GetAxis("Vertical") < 0) {
    104.  
    105.             Vector3 RigRelativeBackwards = CamRig.transform.TransformDirection(-Vector3.forward) * Time.deltaTime;
    106.             /*vertical = Input.GetAxisRaw("Vertical") * Time.deltaTime;
    107.             Vector3 movement = new Vector3(0.0f, 0.0f, vertical);*/
    108.             if (movementSpeed > minSpeed) {
    109.  
    110.                 movementSpeed -= Acceleration;
    111.  
    112.             }
    113.  
    114.             rb.AddForce(RigRelativeBackwards * SlowDownMulti);// -movement * 2
    115.      
    116.         }
    117.  
    118.     }
    119.  
    120.     void PlayerEvade() {
    121.  
    122.         if (Input.GetAxis("Horizontal") != 0) {
    123.  
    124.             horizontal = Input.GetAxisRaw("Horizontal") * EvadeSpeed * Time.deltaTime;
    125.             Vector3 evade = new Vector3(horizontal, 0, 0);
    126.             rb.AddForce(evade); // * EvadeSpeed
    127.  
    128.         }
    129.  
    130.     }
    131.  
    132.     void PlayerRotation() {
    133.  
    134.         transform.Rotate(Vector3.right, movementSpeed / 50);
    135.  
    136.     }
    137.  
    138.     void evadeSpeedRegulator() {
    139.  
    140.         if (movementSpeed >= 680) {
    141.  
    142.             EvadeSpeed = maxEvadeSpeed;
    143.      
    144.         }
    145.  
    146.        /* if (movementSpeed < 60 && movementSpeed > 30) {
    147.  
    148.             EvadeSpeed = midEvadeSpeed;
    149.      
    150.         } */
    151.  
    152.         if (movementSpeed < 680) {
    153.  
    154.             EvadeSpeed = minEvadeSpeed;
    155.      
    156.         }
    157.  
    158.     }
    159.  
    160.     public Text VeloText;
    161.  
    162.     void velo() {
    163.  
    164.         VeloText.text = rb.velocity.ToString();
    165.  
    166.     }
    167.  
    168.     void noBackwardsDriving() {
    169.  
    170.         if (rb.velocity.z < 0) {
    171.  
    172.             Vector3 noback = new Vector3(rb.velocity.x, rb.velocity.y, 0.1f);
    173.  
    174.             rb.velocity = noback;
    175.  
    176.         }
    177.  
    178.     }
    179.  
    180. }
    181.