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 Deynox · Dec 06, 2015 at 08:38 PM · android2d gametouch controlscar game

2D car game touch control

Hi, I have a movement script for 2d car like this, and I don't know how to convert it from keyboard to mobile control. If someone can help me, I will be grateful :)

 using UnityEngine; 
 using System.Collections; 
 
 
 public class lJointCarMovement1 : 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
     public float maxFwdSpeed = -1500;
     //max bwd speed
     float maxBwdSpeed = 1500f;
     //the rate at which the car accelerates
     float accelerationRate = 1400;
     //the rate at which car decelerates
     float decelerationRate = -300;
     //how soon the car stops on braking
     float brakeSpeed = 2000f;
     //acceleration due to gravity
     float gravity = 9.81f;
     //angle in which the car is at wrt the ground
     float slope = 0;
     //reference to the wheels
     public Transform rearWheel;
     public Transform frontWheel;
     
     // Use this for <span id="IL_AD2" class="IL_AD">initialization</span> 
     void Start () { 
         //set the center of mass of the car
         GetComponent<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[1].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){ 
             GetComponent<Rigidbody2D>().AddTorque(3*Mathf.PI*torqueDir, ForceMode2D.Force);
         } 
         else{
             GetComponent<Rigidbody2D>().AddTorque(0);
         }
         
         //determine the cars angle wrt the horizontal ground
         slope = transform.localEulerAngles.z;
         
         //convert the slope values greater than 180 to a negative value so as to add motor speed 
         //based on the slope angle
         if(slope>=180)
             slope = slope - 360;
         //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((slope * 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 && slope < 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((slope * 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 && slope > 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((slope * 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[1].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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Touch input broken with multi-touch 1 Answer

Unity + Othello 2D VS AndEngine 0 Answers

Touch controls no longer work on movement script,2D movement script not working with mobile touch 0 Answers

Help with touch input. 0 Answers

How to flip, on click a constant moving 2d rigidbody on the x-axis and maintain speed? 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