Object stops turning when colliding

Hi, I am facing a problem where my object(car) is being stopped every time it collides with an other object. I have a platform and a car on top of it and both the car and platform is turning(the car only turns when in contact with the platform). So if I stand in front of the car(or just put a cube in the way) the car suddenly stops like anything would do, but when I step away it doesn’t rotate any more. In the scene editor if I apply a quick force in the up direction the car turns again. If this wold help I am making a car that rotates on an platform for a car showroom.
This is the code attached to the car:

using UnityEngine;
using System.Collections;
 
public class TurnCar : MonoBehaviour {
 
    private bool trigger;
    private float turnSpeed = 10;
 
    // Use this for initialization
    void Start ()
    {
 
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(trigger == true)
        {
            transform.Rotate (Vector3.up, turnSpeed*Time.deltaTime);
        }
    }
 
    void OnCollisionEnter(Collision col)
    {
        if (col.collider.tag == "TurnTable")
        {
            trigger = true;
        }
 
        else
            trigger = false;
    }
}

And this is the code for the platform:

using UnityEngine;
using System.Collections;
 
public class TurnObjectStand : MonoBehaviour {
 
   
 
    // Use this for initialization
    void Start ()
    {
 
    }
 
    // Update is called once per frame
    void Update ()
    {
   
        transform.Rotate (Vector3.up, 10f*Time.deltaTime);
    }
}

Any solutions, I have one but it may be unnesesary and can cause problems The solution I have in mind: So the car stops turning when it hits the player or object, what if I apply a small force upwards that will lift it about 3cm(or an inch) once the player steps of the platform so that the car can rotate again. But it still doesn’t solve the problem that why the car stops moving
Thank you

I’m not entirely sure what the intended behaviour is. Do you want the car to stop when hit, and resume rotating when the other object is removed? Or do you intend for it to always rotate as long as it’s in contact with the platform?

I’ll try to explain what’s happening so far, maybe it will help:

With the code you posted, whether or not the car will rotate depends on the last object it has collided with. I assume it will initially “drop” onto the platform, causing a collision and setting “trigger” to true. Collision with any object not tagged “TurnTable” will obviously stop the rotation. Even if you remove the other object, it will require another collision with the platform to set “trigger” to true again, which won’t happen unless you give either the car or the platform a little push.

If you intend the car to always rotate (keeping in sync with the platform), there is no reason to set “trigger” to false on collision. Change that, and the script, while a bit awkward, will do the job.
Expanding on this, you could use OnCollisionExit to determine whether the car lost contact to the platform (if that is at all possible). Not entirely sure how reliable that is, though. Might even depend on your physics settings.

If you intend the car to stop when touched, but resume rotating afterwards, things become slightly more complicated.

I’d recommend letting the engine handle the actual movement, either by making the platform a parent of the car (you can still un-parent if needed), so the car will rotate with it, or by rotating the platform via a force and letting the physics engine do its job.

Hope that helps.

1 Like

Thanks, removing the false worked. But coming to think of it, it would be nice for the car to stop turning once collide with the player and resume when stepping off. I will implement a feature in the code when the player is in contact with the car, the car will stop and visa versa.