Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Controlled drift rigidbody

Discussion in 'Physics' started by ba3uk, Sep 26, 2017.

  1. ba3uk

    ba3uk

    Joined:
    Jul 15, 2017
    Posts:
    7
    Hi,gays.
    Sorry for my English (Russian)
    A task:
    Write a script for driving, without using a wheel collider, only rigidbody. But you need to add the ability to drift.

    There have been several attempts, but in one of them I have not been able to implement the drift itself, but in another one the car is almost not moving in the forward direction.

    attempt - 1
    Code (CSharp):
    1. //MoveCar
    2. Vector3 movement = transform.forward * Time.deltaTime * speed * 1f;
    3. rb.MovePosition(movement + transform.position);
    4. rb.isKinematic = true;

    5. // TurnCar
    6. float angel = Input.GetAxisRaw("Horizontal") * turnSpeed * Time.deltaTime;
    7. Quaternion rot = Quaternion.Euler(0, angel, 0);
    8. rb.MoveRotation(rot * transform.rotation);
    attempt - 2
    Code (CSharp):
    1. if (CheekSpeed(rb.velocity , MaxSpeed))
    2. {
    3. rb.AddRelativeForce(speed * Vector3.forward);
    4. }
    5. float angel = Input.GetAxisRaw("Horizontal") * turnSpeed ;
    6. rb.AddRelativeTorque(Vector3.up * angel , ForceMode.Impulse);


    Tell me what I'm doing wrong.
    Thanks!
     
  2. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    A "drift" is a complicated scenario for your implementation to tackle. And it really depends on how you want your drift to behave. I mean, you could make a realistic drift (not easy with your implementation, you'd want to rely on the physics engine more for this) or you could be making a drift more like Mario Kart.

    If you're aiming for a realistic drift, you need to realize what a drift is, and what causes it in the first place. Here is a helpful article I found on the subject.

    The drift is not just caused by over-steering but relies on your car's ability to "break traction" with the ground. You need to come up with a way of either simulating this with "brute force code" like in your Example-1 code where you'll be manipulating the effect directly, or you need to make your car rely more heavily on the physics engine and tweak the physics involved to allow this effect naturally (will probably require separate colliders for your wheels and the car body).

    Also be aware that this is not an easy problem for most people that are new to game development. People have created many forum posts about it, as well as many projects that focus solely on drift mechanics.
     
  3. ba3uk

    ba3uk

    Joined:
    Jul 15, 2017
    Posts:
    7

    I agree with you, at this stage there is not enough knowledge to write a realistic drift. I wanted to do something like Mario Map 8 or Asphalt 8. Perhaps you have an article on this topic, but I still can not find anything like this or a link to the forum.
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Your attempt 1 seems somewhat close to what your're looking for (forgiving some novice implementation details). I guess that in that attempt the car moves and rotates but it doesn't slide laterally, right?

    The key is here:

    Code (csharp):
    1. Vector3 movement = transform.forward * Time.deltaTime * speed * 1f;
    This means that the car will always move in whatever direction is facing. So if you rotate the car, the movement will follow that direction.

    You may just move the car in a different direction, not the direction it's facing. For example:

    Code (csharp):
    1. Vector3 movementDir = transform.forward * Quaternion.AngleAxis(driftAngle, Vector3.up);
    2. Vector3 movement = movementDir * Time.deltaTime * speed * 1f;
    If you expose and modify the driftAngle parameter the car will be moving like drifting. You may calculate the value in runtime based on your game's rules and situations.

    By separating the car's movement direction from the forward direction you may eventually achieve a rather good drifting effect.
     
    samooniyakk, MoribitoMT and Plystire like this.