Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Happeloy · Sep 23, 2019 at 11:18 AM 0
Share

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.

avatar image xxmariofer Happeloy · Sep 23, 2019 at 01:41 PM 0
Share

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);

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to measure forward velocity - InverseTransformDirection not working ?! 1 Answer

Why do I get different rigidbody.velocity depending on angle? 1 Answer

Cannot move and jump with rigid body. 0 Answers

My player goes straight through my colider 3 Answers

Rigidbody velocity and transform.forward 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges