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 HunterCoker · Oct 24, 2019 at 10:25 PM · errormovementmovement scriptmove an object

How do I make GameObject actions ease in and out

I'm trying to make a car game with a forward and reverse mechanic that ease into its max speed and ease out into a stop. Current code: (Everything labeled)

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Movement : MonoBehaviour {

 public Rigidbody rb;
 public bool grounded = true;

 void OnCollisionEnter(UnityEngine.Collision collisionInfo)
 {
     grounded = true;
 }
 void OnCollisionExit(UnityEngine.Collision collisionInfo)
 {
     grounded = false;
 }
 
 //Start is called before the first frame update
 void Start()
 {
 }

 // Update is called once per frame
 void Update()
 {
     float MaxForwardSpeed = 20f; // maximum speed for acceleration and reverse
     float TurningRadius = 80f; // maximum speed car can turn
     float Speed = 0; // variable that changes the speed
     float Jump = 10f; // not important

     if (grounded == true) //on ground
     {
         if (Input.GetKey(KeyCode.W)) // acceleterate
         {                
             
             // ATTEMPTED GRADUAL ACCELERATION /////////////////////////////////////////////////////
             if ((Speed < MaxForwardSpeed))
             {
                 Speed += 2;
                 transform.position += transform.forward * Time.deltaTime * Speed;
             }
             Debug.Log(Speed);
             ///////////////////////////////////////////////////////////////////////////////////////

             if (Input.GetKey(KeyCode.A)) // only turns when going forward
             {
                 transform.Rotate(0, -TurningRadius * Time.deltaTime, 0);
             }
             if (Input.GetKey(KeyCode.D))
             {
                 transform.Rotate(0, TurningRadius * Time.deltaTime, 0);
             }
         }
         

         if (Input.GetKey(KeyCode.S)) // reverse
         {
             transform.position -= transform.forward * Time.deltaTime * MaxForwardSpeed;

             if (Input.GetKey(KeyCode.A)) // only turns when going backward
             {
                 transform.Rotate(0, -TurningRadius * Time.deltaTime, 0);
             }
             if (Input.GetKey(KeyCode.D))
             {
                 transform.Rotate(0, TurningRadius * Time.deltaTime, 0);
             }
         }
         
         //if (Input.GetMouseButtonDown(1))
         if (Input.GetKeyDown(KeyCode.Space)) // jump function
         {
             rb.AddForce(transform.up * Jump, ForceMode.Impulse);
         }
         if (Input.GetKey(KeyCode.LeftShift)) // air roll toggle(hold)
         {
             if (Input.GetKey(KeyCode.A)) // drift left
             {
                 transform.Rotate(0, -1, 0);
             }
             if (Input.GetKey(KeyCode.D)) // drift right
             {
                 transform.Rotate(0, 1, 0);
             }
         }
     }

     if (grounded == false) //off ground
     {
         if (Input.GetKey(KeyCode.W)) // push forward
         {
             transform.Rotate(1, 0, 0);
         }
         if (Input.GetKey(KeyCode.A)) // car turns left in air
         {
             transform.Rotate(0, -1, 0);
         }
         if (Input.GetKey(KeyCode.S)) // pull back
         {
             transform.Rotate(-1, 0, 0);
         }
         if (Input.GetKey(KeyCode.D)) // car turns right in air
         {
             transform.Rotate(0, 1, 0);
         }
         if (Input.GetKey(KeyCode.LeftShift)) // air roll toggle(hold)
         {
             if (Input.GetKey(KeyCode.A)) // air roll left
             {
                 transform.Rotate(0, 1, 0); // counters other "(KeyCode.A) movement"
                 transform.Rotate(0, 0, 1);
             }
             if (Input.GetKey(KeyCode.D)) // air roll right
             {
                 transform.Rotate(0, -1, 0); // counters other "(KeyCode.D) movement"
                 transform.Rotate(0, 0, -1);
             }
         }
     }
 }

}

  • sorry so much code, I've been working on this for hours and just started using unity a day ago (don't know how to make more compact code)

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

· Add your reply
  • Sort: 
avatar image
0

Answer by theratboy · Oct 24, 2019 at 11:38 PM

Look into lerp. There's also settings called gravity under edit>project settings>input. Gravity makes a input go from 0 (off) to 1 (on) more slowly or fast. if you want smoother movement change your getkey to getaxis and customize the gravity settings from the project settings page in the editor.

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

202 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 can i make it so that the object doesnt instantly go to top speed. Here is my code 1 Answer

How to drag a game object with a mouse (along x axis)? 1 Answer

How can I add a delay between moving objects ? 1 Answer

move enemy to old position of player 2 Answers

Problem with player bouncing or jumping 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