Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
This question was closed Mar 09, 2021 at 03:20 PM by GTvsForza for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GTvsForza · Feb 19, 2021 at 03:03 PM · accelerationcar game

Adding Acceleration to Simple Car Controller

I am currently working on a simple arcade style car game. I looked up a tutorial for a simple controller without the use of wheel colliders. It has a Top Speed variable, but no Acceleration, and I don't know how to implement it. Here is the code:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CarDrive : MonoBehaviour
 {
     private Rigidbody rb;
     public float topSpeed;
     // public float acceleration;
     public float turnSpeed;
     private float currentSpeed;
     public float gravity;
     public GameObject lights;
     public Text speedText;
 
     public bool lightToggle;
     private bool isPressed;
 
     private bool IsPressed
     {
         get { return isPressed; }
         set
         {
             if(value == isPressed)
             {
                 return;
             }
 
             isPressed = value;
 
             if(isPressed)
             {
                 lightToggle = !lightToggle;
                 lights.SetActive(lightToggle);
             }
         }
     }
     
     public float Speed
     {
         get
         {
             return currentSpeed;
         }
     }
     
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     private void Update()
     {
         IsPressed = Input.GetKey(KeyCode.L);
 
         var mph = currentSpeed * 2.237f;
         speedText.text = mph.ToString("F0");
     }
 
     void FixedUpdate()
     {
         currentSpeed = rb.velocity.magnitude;
 
         if (Input.GetKey(KeyCode.UpArrow))
         {
             rb.AddRelativeForce(new Vector3(Vector3.forward.x, 0, Vector3.forward.z) * topSpeed);
         }
         else if (Input.GetKey(KeyCode.DownArrow))
         {
             rb.AddRelativeForce(new Vector3(Vector3.forward.x, 0, Vector3.forward.z) * -topSpeed * 0.25f);
         }
 
         Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
         localVelocity.x = 0;
         rb.velocity = transform.TransformDirection(localVelocity);
 
         if(rb.velocity.magnitude > topSpeed)
         {
             rb.velocity = rb.velocity.normalized * topSpeed;
         }
 
         if(Input.GetKey(KeyCode.RightArrow))
         {
             rb.AddTorque(Vector3.up * turnSpeed * 10);
         } 
         else if (Input.GetKey(KeyCode.LeftArrow))
         {
             rb.AddTorque(-Vector3.up * turnSpeed * 10);
         }
 
         rb.AddForce(Vector3.down * gravity * 10);
     }
 }
 



I'm sure it's an easy implementation, but I'm just not sure where it could go.

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

1 Reply

  • Sort: 
avatar image
0

Answer by GTvsForza · Mar 09, 2021 at 03:19 PM

I figured it out (kind of). When I divided topSpeed by the Rigidbody's mass, it makes it accelerate slower than usual. I guess I could also multiply it by an acceleration factor to make it more realistic, but it's an arcade game, so not super worried about it right now. So, if someone could close this question, that would be great.

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

Follow this Question

Answers Answers and Comments

155 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

Related Questions

Constant Mouse Speed for all Clients 0 Answers

FPSController - Smooth Movement 0 Answers

How to fix this? C# 1 Answer

Problem in steering with wheelcolliders 0 Answers

Where should the Acceleration Go?! 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