1
\$\begingroup\$

I'm making hill climb racing type game but I have an issue here when climbing the car is so slow also there will be stuck between sprite shape like that

enter image description here

enter image description here

  • I've tried configuring hingejoint 2d but it's not working and still stuck
  • Making value some MotorPower bigger but its still slow when climbing

here my car controller script:

JointMotor2D motorBack;
public WheelJoint2D motorWheel;

public Rigidbody2D carRigidbody;

public M_GameManager _GameManager;

public float speed;
float currentSpeed;
public bool isGrounded;
public float groundDistance = 2.1f;

[Header("Power")]
public float motorPower = 1400f, brakePower = 14f, declarationSpeed = .3f;


public float carMaxSpeed = 14f;
float motorTemp;
float powerTemp;
public bool isMobile;

public bool isGas;
public bool isRealesed;

[Header("Particle")]
public ParticleSystem wheelParticle;
ParticleSystem.EmissionModule em;
public Transform particlePosition;

[Header("Fuel")]
public float fuel = 100f;
public float fuelconsumption = 5f;

// Can rotate option. be true value when car is on the fly
bool canRotate = false;

// Rotate force on the  fly 
public float RotateForce = 140f;

void Start()
{
    Application.targetFrameRate = -1;

    _GameManager = FindObjectOfType<M_GameManager>();

    // Set car rigidbody's COM
    carRigidbody = gameObject.GetComponent<Rigidbody2D>();

    // Starting with WheelJoint2D motor
    motorBack = motorWheel.motor;
    //motorFront = motorWheel1.motor;

    // Cast a ray to find isGrounded 
    StartCoroutine(RaycCast());

    //EngineSoundS = GetComponent<AudioSource>();

    powerTemp = motorPower;

    em = wheelParticle.emission;
    em.enabled = false;
}
//float maxspeed = 300f;

void FixedUpdate()
{

    // speed limiter based on max speed limit value
    if (speed > carMaxSpeed)
        motorPower = 0;
    else
        motorPower = powerTemp;

    // Moving forward
    if (Input.GetAxis("Horizontal") > 0 || HoriTemp > 0)
    {

        // Add force to car back wheel
        if (isGrounded)
        {
            motorBack.motorSpeed = Mathf.Lerp(motorBack.motorSpeed, -motorPower, Time.deltaTime * 1.4f);
            fuel -= fuelconsumption * Time.fixedDeltaTime;

            if (fuel < 0)
            {
                _GameManager.isTimerunning = false;
                _GameManager.fuelFinished = true;
                _GameManager.StartFuelFinish();
            }

            if (fuel > 100f)
            {
                fuel = 100;
            }
            //motorFront.motorSpeed = Mathf.Lerp(motorFront.motorSpeed, -motorPower, Time.deltaTime * 1.4f);
        }

        // Wheel particles
        if (isGrounded)
        {
            if (speed < 2f || speed < -2f)
            {
                wheelParticle.transform.position = particlePosition.position;

                em.enabled = true;

            }
            else
                em.enabled = false;
        }
        else
            em.enabled = false;

    }
    else
    {// Moving backward
        if (Input.GetAxis("Horizontal") < 0 || HoriTemp < 0)
        {
            if (speed < -carMaxSpeed)
            {
                if (isGrounded)
                {
                    motorBack.motorSpeed = Mathf.Lerp(motorBack.motorSpeed, 0, Time.deltaTime * 3f);
                    //motorFront.motorSpeed = Mathf.Lerp(motorFront.motorSpeed, 0, Time.deltaTime * 3f);
                }
            }
            else
            {
                if (isGrounded)
                {
                    motorBack.motorSpeed = Mathf.Lerp(motorBack.motorSpeed, motorPower, Time.deltaTime * 1.4f);
                    //motorFront.motorSpeed = Mathf.Lerp(motorFront.motorSpeed, motorPower, Time.deltaTime * 1.4f);
                }

            }

        }
        else
        {
            // Releasing car throttle and brake
            if (isGrounded)
            {
                motorBack.motorSpeed = Mathf.Lerp(motorBack.motorSpeed, 0, Time.deltaTime * declarationSpeed);
                //motorFront.motorSpeed = Mathf.Lerp(motorFront.motorSpeed, 0, Time.deltaTime * declarationSpeed);
            }
        }

    }


    motorWheel.motor = motorBack;
    //motorWheel1.motor = motorFront;

    if (canRotate)
    {
        Rotate();
    }

    if (!isMobile)
        HoriTemp = Input.GetAxis("Horizontal");
}

public float HoriTemp;
void LateUpdate()
{

    // Get car speed
    speed = GetComponent<Rigidbody2D>().velocity.magnitude;
    if (Input.GetAxis("Horizontal") < 0 || HoriTemp < 0)
        speed = -speed;
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I found the answer but maybe bad because I didn't find any the good one,

so basically add 2d physics material to the body of the car collider and set the friction to 0 and make the wheel bigger to get easy when the vehicle climbing, for the slow, just add wheelJoint2d and make new variable front tires so frontTires.motor=motor.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .