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 PeteZa94 · Nov 25, 2016 at 07:57 AM · c#movementmobile

3D Endless Runner - Click/Tap to move

Hi. So I'm trying to make a 3d Endless Runner and I've got a script for switching lanes whenever you press A or D. But what I want is to make it work on mobile, so I need the script to detect when you click on the left side of the screen, or the right side. Swiping left/right would be even better.

Here's the script that I've got for switching lanes:

 float input = Input.GetAxis("Horizontal");
         if(Mathf.Abs(input) > deadZone) {
             if(!didChangeLastFrame) {
                 didChangeLastFrame = true; //Prevent overshooting lanes
                 laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
                 if(laneNumber < 0) laneNumber = 0;
                 else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
             }
         } else {
             didChangeLastFrame = false;
         }
 
         Vector3 pos = transform.position;
         pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
         transform.position = pos;

How would I make that script work on clicking left/right side of the screen or swiping left/right?

Here's a little bit of code I've got for detecting if you click/tap on the left/right side of the screen:

 moveVector.x = Input.GetAxisRaw ("Horizontal") * speed;
         if (Input.GetMouseButton (0))
         {
             // Divide by 2 = right side // Else = left side
             if (Input.mousePosition.x > Screen.width / 2)
                 moveVector.x = speed;
             else
                 moveVector.x = -speed;
         }

But the problem with this is that the player moves smoothly, it doesn't switch lanes like in the first script.

BTW. I'm a beginner at this.

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 PeteZa94 · Nov 25, 2016 at 01:17 PM 0
Share

bump please

1 Reply

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

Answer by Garazbolg · Nov 25, 2016 at 02:05 PM

Try this :

  float input = Input.GetAxis("Horizontal");
          if(Input.GetMouseButton(0))                              //Just added these two lines
              input += Input.mousePosition.x*2/Screen.width-1;         // converting mouseX from 
                                                                       // [0;Screen[ to [-1;1[
                                                                       // like the Input.GetAxis()
          if(Mathf.Abs(input) > deadZone) {
              if(!didChangeLastFrame) {
                  didChangeLastFrame = true; //Prevent overshooting lanes
                  laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
                  if(laneNumber < 0) laneNumber = 0;
                  else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
              }
          } else {
              didChangeLastFrame = false;
          }
  
          Vector3 pos = transform.position;
          pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
          transform.position = pos;


With this you should still have a dead zone in the middle of the screen (maybe for other interactions) and still have the behaviour that you had with the keyboard.

Also now on computer you can test the phone interaction by using the mouse as your finger.

Hope it helps you !

Comment
Add comment · Show 6 · 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 PeteZa94 · Nov 25, 2016 at 02:50 PM 1
Share

Thank you man, works perfect!

avatar image Garazbolg PeteZa94 · Nov 25, 2016 at 03:00 PM 0
Share

No problem

avatar image PeteZa94 Garazbolg · Nov 25, 2016 at 05:28 PM 0
Share

Do you know how to make it so that it only works for the bottom half of the screen? So divide the screen by 2 again but the other way and do nothing on the top half.

Show more comments
avatar image Lamphong · Oct 18, 2017 at 02:32 AM 0
Share

deadZone ??

didChangeLastFrame?? what is?? please

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

[Problem] Two Mobile Single Stick Controllers and Click Controls not working synonymous with each other 0 Answers

how to stop rotation from being uncontrollable 1 Answer

Multiple Cars not working 1 Answer

Unity3d 4,script nGUI btn drag convert to move where i mouse click 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