Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 drydrunkpig · Feb 07, 2015 at 01:51 PM · script.wheel

I need help making wheels.

I have a 2d bike using wheel joints but using motors doesnt give me a smooth ride. I need it to accelerate on press and be neutral on release and only slow down by gravity and such. Can anyone help? My current code decelerates and brakes terribly.

 using UnityEngine; 
 using System.Collections; 
  
  
 public class WheelCarJointMovement : MonoBehaviour { 
  //reference to the wheel joints
  WheelJoint2D[] wheelJoints; 
  //center of mass of the car
  public Transform centerOfMass;
  //reference tot he motor joint
  JointMotor2D motorBack;  
  //horizontal movement keyboard input
  float dir = 0f; 
  //input for rotation of the car
  float torqueDir = 0f;
  //max fwd speed which the car can move at
  float maxFwdSpeed = -5000;
  //max bwd speed
  float maxBwdSpeed = 2000f;
  //the rate at which the car accelerates
  float accelerationRate = 500;
  //the rate at which car decelerates
  float decelerationRate = -100;
  //how soon the car stops on braking
  float brakeSpeed = 2500f;
  //acceleration due to gravity
  float gravity = 9.81f;
  //reference to the wheels
  public Transform rearWheel;
  public Transform frontWheel;
  
  // Use this for initialization 
  void Start () { 
   //set the center of mass of the car
   rigidbody2D.centerOfMass = centerOfMass.transform.localPosition;
   //get the wheeljoint components
   wheelJoints = gameObject.GetComponents<WheelJoint2D>(); 
   //get the reference to the motor of rear wheels joint
   motorBack = wheelJoints[0].motor; 
  }  
   
  //all physics based assignment done here
  void FixedUpdate(){
   //add ability to rotate the car around its axis
   torqueDir = Input.GetAxis("Horizontal"); 
   if(torqueDir!=0){ 
    rigidbody2D.AddTorque(3*Mathf.PI*torqueDir, ForceMode2D.Force);
   } 
   else{
    rigidbody2D.AddTorque(0);
   }
 
   //horizontal movement input. same as torqueDir. Could have avoided it, but decided to 
   //use it since some of you might want to use the Vertical axis for the torqueDir
   dir = Input.GetAxis("Horizontal"); 
  
   //explained in the post in detail
   //check if there is any input from the user
   if(dir!=0)
    //add speed accordingly
    motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(dir*accelerationRate - gravity*Mathf.Sin((Mathf.PI)/180)*80 )*Time.deltaTime, maxFwdSpeed, maxBwdSpeed);
   //if no input and car is moving forward or no input and car is stagnant and is on an inclined plane with negative slope
   if((dir==0 && motorBack.motorSpeed < 0 ) ||(dir==0 && motorBack.motorSpeed==0)){
    //decelerate the car while adding the speed if the car is on an inclined plane
    motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed - (decelerationRate - gravity*Mathf.Sin((Mathf.PI)/180)*80)*Time.deltaTime, maxFwdSpeed, 0);
   }
   //if no input and car is moving backward or no input and car is stagnant and is on an inclined plane with positive slope
   else if((dir==0 && motorBack.motorSpeed > 0 )||(dir==0 && motorBack.motorSpeed==0)){
    //decelerate the car while adding the speed if the car is on an inclined plane
    motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(-decelerationRate - gravity*Mathf.Sin((Mathf.PI)/180)*80)*Time.deltaTime, 0, maxBwdSpeed);
   }
    
    
    
   //apply brakes to the car
   if (Input.GetKey(KeyCode.Space) && motorBack.motorSpeed > 0){
    motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed - brakeSpeed*Time.deltaTime, 0, maxBwdSpeed); 
   }
   else if(Input.GetKey(KeyCode.Space) && motorBack.motorSpeed < 0){ 
    motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed + brakeSpeed*Time.deltaTime, maxFwdSpeed, 0);
   }
   //connect the motor to the joint
   wheelJoints[0].motor = motorBack; 
  
  }
  
 }
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
1

Answer by PersianPrime · Jun 01, 2017 at 01:47 PM

You must multiple​ "slope" variable with others in all sine functions. Like this:

 motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(-decelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80)*Time.deltaTime, 0, maxBwdSpeed);

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to gradually slow down the speed of the wheel? 1 Answer

need help with making an enemybase script. I need help with the enemy movement because I have three enemies in total and I need to polymorphically have each enemy attack and move. Thanks 0 Answers

how i can stop the animator to play trigger animation? 1 Answer

[Shooting shots Scripting] How to make the compiler know that I mean the empty quad by the Public Transform? 0 Answers

Loading GameObject with the Script Attached from the AssetBundle. 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