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 naka88sushi_ · Oct 05, 2014 at 09:12 PM · touchracingtap

I want to make a tap to "teleport" code.. is this correct?

hye guys, I want to make a 2D racing game that has only 2 lanes. and I want to make the car(player) change its lane every single tap. From right lane, tap! then left lane. From left lane, tap! then right lane.

so this is my code. but compiler says parser error. expecting a close curly braces "{}" at the bottom.. help me guys! :D

 void Update () 
     {
      bool Left = true ;
         
      Touch tap = Input.GetTouch (0);
      if (Left == true) 
     {
      if (tap.phase == TouchPhase.Ended && tap.tapCount == 1) 
      {
      transform.Translate (Vector2.right * 5 * Time.deltaTime);
      Left = false;
      }
     }
     else if (Left == false)
     {
     if (tap.phase == TouchPhase.Ended && tap.tapCount == 1)
      {
      transform.Translate (Vector2.right * -5 * Time.deltaTime);
      Left = true;
      }
     }
 }
Comment
Add comment · Show 2
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 naka88sushi_ · Oct 06, 2014 at 03:44 AM 0
Share

sorry, but that is the whole script. i just fix the parsing error on line 23.. but i still didnt manage to move my character from left to right and vice versa by a single tap..

avatar image Kiwasi · Oct 06, 2014 at 08:28 AM 0
Share

This is not the whole script. You are writing in C#. C# scripts always contain a class declaration.

(That's a lie, but if you are still fighting parsing errors you don't need to worry about the details for now)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CgShady · Oct 05, 2014 at 09:29 PM

A few comments :

"Left" will always be true as it's declared within the Update method and set to true every frame.

You could do :

     private bool left = true;
     void Update ()
     {
         Touch tap = Input.GetTouch ( 0 );
 
         if ( tap.phase == TouchPhase.Ended && tap.tapCount == 1 )
         {
             if ( left )
             {
                     transform.Translate ( Vector2.right * 5 * Time.deltaTime );
             }
             else
             {
                 transform.Translate ( Vector2.right * -5 * Time.deltaTime );
             }
             left = !left;
         }
     }


or even something like this :

 private bool _left = true;
     public bool Left
     {
         get { return _left ; }
         set
         {
             if (value != _left)
             {
                 _left = value ;
             }
         }
     }
 
     public float Offset
     {
         get
         {
             float offset = Left ? 5 : -5 ;
             Left = !Left ;
             return offset ;
         }
     }
     
     void Update ()
     {
         Touch tap = Input.GetTouch ( 0 );
 
         if ( tap.phase == TouchPhase.Ended && tap.tapCount == 1 )
         {
             transform.Translate ( Vector2.right * Offset * Time.deltaTime );
         }
     }
Comment
Add comment · Show 3 · 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 CgShady · Oct 05, 2014 at 09:39 PM 0
Share

Sorry, I didn't read your question to the end, yes you probably miss a closing } at the end. The ending bracket of your code matches the Update Opening bracket. You need one to close the Class.

avatar image naka88sushi_ · Oct 06, 2014 at 04:01 AM 0
Share

thank you cg shady, but when i type in your code.. why does the private and public get red squigy line? (like when you mispelled something in ms word).

avatar image dmg0600 · Oct 06, 2014 at 06:20 AM 0
Share

public and private have to be outside any method. That's why he is declaring private bool left = true; right before Update, outside of it.

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

What's the BEST: most efficient, reliable, cross-platform way to detect a touch/tap on mobile (Android-iOS) on several 2D colliders in a scene while still retaining desktop compatibility and raycast/touch masking? 0 Answers

Differentiating a tap from a swipe? 1 Answer

Single Tap Registered on Swipe (C#) 0 Answers

codes for detecting mobile double tap and hold 1 Answer

touch game objects on ios 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