• 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 TheRockeR93 · Aug 16, 2016 at 05:17 AM · unity 52duiphysicscar

Unity 2D Car Side Scroller question.

So I'm making a 2D Car Side Scroller for Android. I've got the car working with colliders and all. But the script I'm stuck on.

I've got a working car script, but it only takes in inputs from keyboard and I'm looking for a way to move with buttons. I've already set up a Brake and Accelerate button UI now I just need the script. And I've tried so many things, tried all the Input.touches, GetTouch. And I couldn't find any help online so I thought I'd ask here.

Here's how the script works if anyone can help me manage to get it working with the Unity UI for touch input.

 using UnityEngine; 
 using System.Collections; 
 
 
 public class WheelJointCarMovement : MonoBehaviour {
     WheelJoint2D[] wheelJoints;
     public Transform centerOfMass;
     JointMotor2D motorBack;
     public float dir = 0f;
     float torqueDir = 0f;
     float maxFwdSpeed = -5000;
     float maxBwdSpeed = 2000f;
     float accelerationRate = 500;
     float decelerationRate = -100;
     float brakeSpeed = 3500f;
     float gravity = 9.81f;
     float slope = 0;
     public Transform rearWheel;
     public Transform frontWheel;
 
     // Use this for initialization 
     public void Start () {
         GetComponent<Rigidbody2D>().centerOfMass = centerOfMass.transform.localPosition;
         wheelJoints = gameObject.GetComponents<WheelJoint2D>();
         motorBack = wheelJoints[0].motor;
         motorBack = wheelJoints[1].motor;
     }  
 
     //all physics based assignment done here
     public void FixedUpdate(){
         torqueDir = Input.GetAxis("Horizontal");
         if(torqueDir!=0){ 
             GetComponent<Rigidbody2D>().AddTorque(3*Mathf.PI*torqueDir, ForceMode2D.Force);
         } 
         else{
             GetComponent<Rigidbody2D>().AddTorque(0);
         }
             
         slope = transform.localEulerAngles.z;
 
         if(slope>=180)
             slope = slope - 360;
         dir = Input.GetAxis("Horizontal"); 
 
         if(dir!=0)
             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(dir*accelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80 )*Time.deltaTime, maxFwdSpeed, maxBwdSpeed);
     
         if((dir==0 && motorBack.motorSpeed < 0 ) ||(dir==0 && motorBack.motorSpeed==0 && slope < 0)){
             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed - (decelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80)*Time.deltaTime, maxFwdSpeed, 0);
         }
         else if((dir==0 && motorBack.motorSpeed > 0 )||(dir==0 && motorBack.motorSpeed==0 && slope > 0)){
             motorBack.motorSpeed = Mathf.Clamp(motorBack.motorSpeed -(-decelerationRate - gravity*Mathf.Sin((slope * Mathf.PI)/180)*80)*Time.deltaTime, 0, maxBwdSpeed);
         }
 
 
 
 
         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);
         }
         wheelJoints[0].motor = motorBack;
         wheelJoints[1].motor = motorBack;
 
     }
 }
 
Comment
Add comment · Show 1
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 TheXWolf · Dec 06, 2016 at 12:58 AM 0
Share

You stated you're trying to use buttons, do you mean physical buttons on a controller or UI based buttons on the screen. If you're using UI based buttons you need to sent the update changing the speed when the button is pressed. If you're trying to use a controller, considering using CrossPlatformInput$$anonymous$$anager.GetAxis or .GetButton.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nova-1504 · Dec 06, 2016 at 01:19 AM

I'm no expert, but i'm pretty sure at the top you need using UnityEngine.UI;.

Comment
Add comment · Show 2 · 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 TheXWolf · Dec 06, 2016 at 01:23 AM 0
Share

That's incorrect, UnityEngine.UI only relates to Components added for the Unity uGUI system, nothing on this script refers to the GUI directly. i.e. Text, Button, Image, Slider, etc.

avatar image Nova-1504 TheXWolf · Dec 06, 2016 at 01:25 AM 0
Share

Ok, sorry. Carry on.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI Collision Detection 0 Answers

Maintain equal physics over different UI sizes? 2 Answers

A way to create/draw 2D game map. 1 Answer

Car Syncing Problems with Photon 0 Answers

I want UI to be displayed when shifting gears. 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