Search Unity

My car can't go up ramps very well

Discussion in 'Scripting' started by Epikmemer45, May 15, 2022.

  1. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    Hello. I'm trying to make a driving game, but I can't, since I can't get the vehicle to go up ramps well. Can anyone help fix this?

    Here is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CarControlla : MonoBehaviour
    6. {
    7.     public Rigidbody arbees;
    8.     public Transform[] WheelMeshes;
    9.     public WheelCollider[] WheelColliders;
    10.     Vector3 pos, rotation;
    11.     Quaternion quat;
    12.     public float Toque, RotSpeed;
    13.     public float addDownForceValue = 50;
    14.     public GameObject mas;
    15.  
    16.     void Start()
    17.     {
    18.         rotation = transform.eulerAngles;
    19.         arbees = gameObject.GetComponent<Rigidbody>();
    20.         mas = GameObject.Find("mASS");
    21.         arbees.centerOfMass = mas.transform.localPosition;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void FixedUpdate()
    26.     {
    27.         MoveDaCar();
    28.         SteerDaCar();
    29.     }
    30.  
    31.     private void AddDownForce()
    32.     {
    33.         arbees.AddForce(-transform.up * addDownForceValue * arbees.velocity.magnitude);
    34.     }
    35.  
    36.     private void MoveDaCar()
    37.     {
    38.         for (int i = 0; i < WheelColliders.Length; i++)
    39.         {
    40.             WheelColliders[i].GetWorldPose(out pos, out quat);
    41.             WheelMeshes[i].position = pos;
    42.             WheelMeshes[i].rotation = quat;
    43.         }
    44.  
    45.         foreach (var wheelcol in WheelColliders)
    46.         {
    47.             wheelcol.motorTorque = Input.GetAxis("Vertical") * Toque * 6 * Time.deltaTime;
    48.         }
    49.  
    50.         AddDownForce();
    51.     }
    52.  
    53.     private void SteerDaCar()
    54.     {
    55.         transform.eulerAngles = rotation;
    56.  
    57.         rotation.y += Input.GetAxis("Horizontal") * RotSpeed;
    58.     }
    59.  
    60.  
    61. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    What's the problem?

    Colliders too slippery? Adjust them!

    Wheels not in contact with ground? Adjust wheel suspension travel?

    Not able to get enough force through wheels? Perhaps add an adjunct linear force in synchrony with gas pedal / brakes?

    Something else? Here's how to find out:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    NOTE: this .y has NOTHING to do with angles. It's an internal component of a Quaternion. Do not manipulate it unless you have a math degree.

    If this is a car with wheels, shouldn't you be steering the wheels through the wheel collider API?

    ALSO: With Physics (or Physics2D), never manipulate the Transform directly (as you are by setting the rotation). If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    With a wheel collider, use the motor or brake torque to effect motion.