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 champion342 · Dec 07, 2019 at 11:35 PM · movementvehicle

How to make an object slowly decelerate?

Im very new to coding so bear with me, im making a car game, with the car being a single block, so no fancy wheels or anything. I have the movement code down, now i need to somehow make it so when i press space bar, the 'car' slowly decelerates to a complete stop. Can someone help?

Heres the code:

 public class scr : MonoBehaviour
 {
     public float thrust = 1.0f;
     public Rigidbody rb;
     public GameObject move;
     public Vector3 offset;
     public float angleBetween = 0.0f;
     public Transform target;
     
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         offset = transform.position - move.transform.position;
     }
 
     //movementcode
     void FixedUpdate()
     {
     if (Input.GetKey(KeyCode.W))
         rb.AddForce(transform.forward * thrust);
     if (Input.GetKey(KeyCode.S))
         rb.AddForce(-transform.forward * thrust);
     if (Input.GetKey(KeyCode.A))
        transform.Rotate(0, -1, 0 * thrust);
     if (Input.GetKey(KeyCode.D))
        transform.Rotate(0, 1, 0 * thrust);
     if (Input.GetKey(KeyCode.LeftShift))
        rb.AddForce(transform.forward * thrust * 2, ForceMode.Acceleration);
     
        
     
        
 
     
     }
   
     void LateUpdate()
     {
      transform.position = move.transform.position + offset;
     }
 }


Thank you!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Anis1808 · Dec 07, 2019 at 11:44 PM

You could try to add a ForceMode.Impulse when you add the force rb.AddForce(/*force*/, ForceMode.Impulse); Or you could simply add a deceleration that you control fully:

 if (Input.GetKey(KeyCode.W)){
          rb.AddForce(transform.forward * thrust);
 }else if (Input.GetKey(KeyCode.S)){
          rb.AddForce(-transform.forward * thrust);
 }else{ //If no Input
         rb.velocity = rb.velocity * 0.95f * Time.deltaTime;      //Edit 0.95 with the value you want as long as it is smaller than 1 and bigger than 0
 }
Comment
Add comment · Share
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
0

Answer by RadonRaph · Dec 08, 2019 at 10:12 AM

Hello @champion342,

If you use rigidbody simply increase the drag property and angular drag, you could also edit the physic material.

What you want is friction, to add friction simply substract a percent of the velocity each frame (like in late update) like this:

 rb.velocity -= 0.1f*rb.velocity;


Hope that help,

Raph

Comment
Add comment · Share
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

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

176 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 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

vehicle not accelerating after braking 2 Answers

How to play sound when object stops moving? 1 Answer

vehicle moveing on the terrain 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

YES/NO Can I implement a bouncing effect on my vehicle using physics joints 0 Answers


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