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 Fazle_Rabbi · Aug 16, 2015 at 04:00 PM · android

How to restrict touch movement for android.

Hey, i am working on a 2D car racing game for android device. I have coded the touch movement for my car. But the problem is the car is going beyond the track. How can i restrict the car movement, i mean how can i code for my car to stay in the screen(Screen resulation is 480*800 and car sprites max position is 4.2 and min -4.2). Here r my C# car controller script. using UnityEngine; using System.Collections;

public class carController : MonoBehaviour {

 public float carSpeed;
 

 // Update is called once per frame
 void Update () {

     if (Input.touchCount == 1) {
     
         Touch touch = Input.touches[0];
         if(touch.position.x < Screen.width/2){
             transform.position += Vector3.left * carSpeed * Time.deltaTime;


         }
         else if(touch.position.x > Screen.width/2){
             transform.position += Vector3.right * carSpeed * Time.deltaTime;

         }
     
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Evansbsr · Aug 16, 2015 at 06:26 PM

Use colliders to define the boundaries of the track

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 Fazle_Rabbi · Oct 01, 2015 at 07:26 AM 0
Share

Thanks for your answer. I have tried that solution. But the problem is it's actually working in case of single touch. If i touch for a long time my car gone beyond the street and the boundaries failed to stay the car inside the screen.

avatar image Evansbsr · Oct 01, 2015 at 11:06 AM 0
Share

Store the x position in a temporary variable and clamp this to the boundaries you'd like before applying it to the transform

  public float carSpeed;
  
 float _x;
 
  // Update is called once per frame
  void Update () {
      if (Input.touchCount == 1) {
         _x = transform.position.x;
 
          Touch touch = Input.touches[0];
          if(touch.position.x < Screen.width/2){
              _x -= carSpeed * Time.deltaTime;
          }
          else if(touch.position.x > Screen.width/2){
              _x += carSpeed * Time.deltaTime;
          }
        
          _x = $$anonymous$$athf.Clamp(_x, -4.2f, 4.2f);
 
         transform.position = new Vector3(_x, transform.position.y, transform.position.z);
      }
  }

avatar image
0

Answer by saschandroid · Oct 01, 2015 at 08:58 AM

 if(touch.position.x < Screen.width/2)
 {
     transform.position += Vector3.left * carSpeed * Time.deltaTime;
     if ( transform.position.x < -4.2f )
     {
         transform.position = new Vector3(-4.2f, transform.position.y, transform.position.z);
     }
 }
 else if(touch.position.x > Screen.width/2)
 {
     transform.position += Vector3.right * carSpeed * Time.deltaTime;
     if ( transform.position.x > 4.2f )
     {
         transform.position = new Vector3(4.2f, transform.position.y, transform.position.z);
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How To make unity work in Hebrew ? 1 Answer

How to prevent Android from killing my Unity app ? 1 Answer

Export & Install APK on Android 5 Answers

algorithm for 3D simulation racing 1 Answer

Unity 4.3 Android, Entire Screen Not Displayed on Device 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