Sejpalsinh Jadeja
Sejpalsinh Jadeja

Reputation: 355

Make Bike stable in Unity 2D game

I'm developing 2D game somewhat like Hill climb Racing with the bike and For jointing of Wheel I'm using Wheel Joint 2D and for movement, I'm using

rb.AddTorque(rotation * rotationSpeed * Time.fixedDeltaTime);

enter image description here

It's working fine but after getting speed Bike is vibrating and Wheel leaves its original joint place.

I'm following this tutorial link

Upvotes: 2

Views: 1715

Answers (1)

Srushti Khunt
Srushti Khunt

Reputation: 186

I have the same problem with this tutorial and i resolve it by using motor instead AddTorque And if your wheel leaves it's original place. Zoom it and make sure the Wheel Joint 2D is perfectly in center of wheel otherwise it will vibrate on place.

For this tutorial I resolve it by using this code for BikeController.

using UnityEngine;
public class BikeController : MonoBehaviour
{
    public float speed = 1500;
    public float rotationSpeed = 10f;
    public WheelJoint2D backWheel;
    public WheelJoint2D frontWheel;
    public Rigidbody2D rb;
    private float movement = 0f;
    private float rotation = 0f;
    public Transform player;
    void Update()
    {
        movement = Input.GetAxisRaw("Horizontal") * speed; 
        rotation = Input.GetAxisRaw("Vertical");
        if (Input.touchCount > 0)
        {
            Touch tch = Input.GetTouch(0);
            if (tch.position.x > player.position.x)
            {
                movement = speed;
            }
            if (tch.position.x < player.position.x)
            {
                movement = -speed;
            }
        }
    }
    void FixedUpdate()
    {
        if (movement == 0f)
        {
            backWheel.useMotor = false;
            frontWheel.useMotor = false;
        }
        else
        {
            backWheel.useMotor = true;
            frontWheel.useMotor = true;
            JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = 10000 };
            backWheel.motor = motor;
            frontWheel.motor = motor;
        }
        rb.AddTorque(rotation * rotationSpeed * Time.fixedDeltaTime);
    }
}

Upvotes: 4

Related Questions