Need help rotating object

I’m making a Mario Kart clone but don’t know how to make the kart works, the wheel colliders don’t work in my case, so I decided to make everything in code, and when I press A and D the kart should rotate in y axis, but it rotates slowly in the z axis and turn upside down and continues rotating forever

I’m using rigidbody and capsule collider in the kart

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public float speed = 7.0f;
    public float rotSpeed;
    public float zero = 0.0f;

    public Animator anim;

    void Start()
    {
        anim = GetComponent<Animator> ();
    }

    void Update()
    {

        if (Input.GetKey (KeyCode.W))
        {
            anim.SetInteger("condition", 1);

            transform.Translate(0.0f, 0.0f, speed * Input.GetAxis("Vertical") * Time.deltaTime);

        }
        if(Input.GetKeyUp (KeyCode.W))
        {
            anim.SetInteger("condition", 0);
        }

        if (Input.GetKey (KeyCode.S))
        {
            anim.SetInteger("condition", 2);

            transform.Translate(0.0f, 0.0f, speed * Input.GetAxis("Vertical") * Time.deltaTime);
        }
        if (Input.GetKeyUp(KeyCode.S))
        {
            anim.SetInteger("condition", 0);
        }


        if (Input.GetKey (KeyCode.D))
        {
            transform.Rotate(Vector3.up * rotSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.up * -rotSpeed * Time.deltaTime);
        }
    }
}

i don’t quite have time to get into it at the moment, but you’ll want to check this thread out. Differences between transform.Translate, rigidbody.Velocity and rigidbody.MovePosition? theres a good amount of tutorials on youtube, but this is the best direction to send you in for now.

do you have time now? the post didn’t helped me and I dont know what to do

This is the official unity tutorial for AddForce. If you’re planning to use physics you’re going to want to use AddForce instead of translate, otherwise you’ll have objects that may ignore collisions.

This is the tutorial for AddTorque. If you’re having trouble with your object continuing to rotate you’re going to want to adjust your drag variable.

if you really don’t want to use physics and keep doing it the way you’re doing it, this script will do what you were trying to accomplish, but its basically garbage and you should really try to learn from the tutorials I’ve provided above.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Kart : MonoBehaviour
{

    GameObject kart;
    public float force;
    public float torque;

    // Update is called once per frame

    private void Start()
    {
        kart = this.gameObject;
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.W)) {

            kart.transform.Translate(0, 0, force * Time.deltaTime);
          
        }

        if (Input.GetKey(KeyCode.S))   {

            kart.transform.Translate(0, 0, -force * Time.deltaTime);
          
        }

        if (Input.GetKey(KeyCode.D))  {

            kart.transform.Rotate (0, torque * Time.deltaTime, 0);
          
        }

        if (Input.GetKey(KeyCode.A)) {

            kart.transform.Rotate(0, -torque * Time.deltaTime, 0);
          
        }


    }
}

just slap it on your box or whatever and you can use standard wasd controls. you’ll want your torque to be about 10 times your force variable.

1 Like

It’s working now but when i go in a ramp the kart starts rotating again and doesn’t stops, do you know something that i can do for solve this?

edit: i have changed the capsule collider to a box collider and now it’s working fine, thxs for all your helping