- Home /
 
 
               Question by 
               AxolotlDreams · Sep 23, 2019 at 08:51 AM · 
                movement scriptrigidbody.velocitytransform.forward  
              
 
              Convert current rigidbody velocity to transform.forward
I'm tuning my drift mechanic so that when the drift is released, the player gets a huge burst of speed in the direction they are currently facing. As is, it does grant a huge burst of speed, but the momentum from previous forces still apply and the player moves like they're on ice.
Here's my code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerControl : MonoBehaviour
 {
     public float speed;
     public float accel = 10.0f;
     public float topspeed = 15.0f;
     public float turnSpeed = .35f;
     public float driftSpeed = 2f;
     public float driftDrag = 5f;
     public float driftBonus;
     public float jumpCharge = 1.0f;
     public bool drift;
     public bool grounded;
     public bool breaking;
     private Rigidbody rb;
     private float turnDir;
     [SerializeField] private float driftLock;
     // Start is called before the first frame update
     void Start()
     {
         rb = this.GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         speed = rb.velocity.magnitude;
         Inputs();
         //Regulate Speed
         if (speed < topspeed+1 && !breaking && grounded)
         {
             Accelerate();
         }
         else if (speed > topspeed)
         {
             breaking = true;
         }
         //Drift Function
         if (drift)
         {
             Drift();
         }
         breaking = false;
     }
 
     private void FixedUpdate()
     {
         if (breaking)
         {
             Slow();
         }
     }
 
     private void Inputs()
     {
         //Turning
         if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > .05)
         {
             transform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed, 0, relativeTo: Space.Self);
             turnDir = Input.GetAxisRaw("Horizontal");
         }
 
         //Drifting
         if (Input.GetKeyDown(KeyCode.LeftShift) && Mathf.Abs(Input.GetAxisRaw("Horizontal")) > .05 && grounded)
         {
             drift = true;
         }
         else if (Input.GetKeyUp(KeyCode.LeftShift) && grounded)
         {
             drift = false;
             //Adds boost
             if (5 <= speed && speed <= topspeed)
             {
                 rb.AddForce(transform.forward * ((topspeed/1.5f) * (driftBonus/10)), ForceMode.Impulse);
             }
             else
             {
                 rb.AddForce(transform.forward * (driftBonus/10), ForceMode.Impulse);
             }
             driftBonus = 0;
             driftLock = 0f;
         }
         //Jump Charging
         if (Input.GetKey(KeyCode.Space) && grounded)
         {
             //charge jump
             if (jumpCharge < 3.5f)
             {
                 jumpCharge += .1f;
             }
             else
             {
                 jumpCharge = 2;
             }
             //slow speed
             if (speed > 3)
             {
                 breaking = true;
             }
             else
             {
                 breaking = false;
             }
         }
         //Jump Release
         else if (Input.GetKeyUp(KeyCode.Space) && grounded)
         {
             rb.AddForce(transform.up * (2 * jumpCharge), ForceMode.VelocityChange);
             jumpCharge = 1f;
         }
     }
 
     private void Drift()
     {
         //Lock Drift Direction
         if (driftLock == 0f)
         {
             driftLock = turnDir;
         }
         //Modify Drift Intensity
         if (Input.GetAxisRaw("Horizontal") == -driftLock)
         {
             driftLock = driftLock + (.25f * Input.GetAxisRaw("Horizontal"));
         }
         else if (Input.GetAxisRaw("Horizontal") == (4/3) * driftLock)
         {
             driftLock = (4/3) * driftLock;
         }
 
         //Drag
         if (speed > 1)
         {
             rb.AddForce(-transform.forward * (speed * driftDrag));
         }
         //Drift
         transform.Rotate(0, driftLock * driftSpeed, 0, relativeTo: Space.Self);
         //Boost
         if (driftBonus <= 20)
         {
             driftBonus += 1f;
         }
 
         if (speed < 3.0f)
         {
             drift = false;
             driftBonus = 0;
         }
 
     }
 
     private void Slow()
     {
         //Reduce to Topspeed
         rb.velocity = rb.velocity * .95f * Time.deltaTime;
         speed = rb.velocity.magnitude;
     }
 
     private void Accelerate()
     {
         //Increase to Topspeed
         speed = rb.velocity.magnitude;
         if (speed < topspeed)
         {
             rb.AddForce(transform.forward * accel);
         }
         else
         {
             rb.velocity = rb.velocity.normalized * topspeed;
         }
     }
 
     private void OnCollisionStay(Collision collision)
     {
         if (collision.collider.gameObject.layer.Equals(8))
         {
             grounded = true;
         }
     }
 
     private void OnCollisionExit(Collision collision)
     {
         if (collision.collider.gameObject.layer.Equals(8))
         {
             grounded = false;
             drift = false;
             driftBonus = 0;
             driftLock = 0f;
         }
     }
 }
 
               The section that handles boosting is Lines 66-85:
 //Drifting
         if (Input.GetKeyDown(KeyCode.LeftShift) && Mathf.Abs(Input.GetAxisRaw("Horizontal")) > .05 && grounded)
         {
             drift = true;
         }
         else if (Input.GetKeyUp(KeyCode.LeftShift) && grounded)
         {
             drift = false;
             //Adds boost
             if (5 <= speed && speed <= topspeed)
             {
                 rb.AddForce(transform.forward * ((topspeed/1.5f) * (driftBonus/10)), ForceMode.Impulse);
             }
             else
             {
                 rb.AddForce(transform.forward * (driftBonus/10), ForceMode.Impulse);
             }
             driftBonus = 0;
             driftLock = 0f;
         }
 
              
               Comment
              
 
               
              Since you use AddForce(), the forces will be added to each other, just as you describe. If you do not want to add the forces together, you could calculate the desired velocity vector and manipulate the rb.velocity-vector directly ins$$anonymous$$d. 
of even simplier just set the rigidbody forces to cero before adding the forces
  rb.velocity = Vector3,zero;
  rb.AddForce(transform.forward * ((topspeed/1.5f) * (driftBonus/10)), Force$$anonymous$$ode.Impulse);
 
                  Your answer