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 /
  • Help Room /
avatar image
0
Question by Michael Yu · Jun 12, 2016 at 07:03 AM · c#2dplatformerendless runner

Moving left and right with one button in 2d Game

Hi, I am working on a runner game where you try to dodge obstacles. What I am trying to do is on a single key press it changes directions. So if it is going left then key press it goes left and vice versa. I made a code but the problem is that it won't switch directions. If you could, please look over and tell what is wrong with the code.

using UnityEngine; using System.Collections;

public class Main : MonoBehaviour {

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Rotate()
 {
     transform.Rotate(Vector3.back * -20);
 }
 void Update () {
     int speed = 1;
     int move = 1;
     if (move == 1)
     {
         transform.Translate(Vector3.right * Time.deltaTime * speed);
         
     }
                 
     if (move == -1)
     {
         transform.Translate(Vector3.left * Time.deltaTime * speed);
         
     }

     if(Input.GetKey("space"))
     {
         move = move * -1;
     }
    
             
     
         

 }

}

P.S. Later I want to make it an app. So how do I set it up so that if you tap the screen(anywhere on the screen) the character will switch directions. Thanks!

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

1 Reply

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

Answer by Mmmpies · Jun 12, 2016 at 07:50 AM

You need 2 bools for this...

     private bool moveRight = true;
     private bool buttonDown = false;
 
     public void ButtonIsDown()
     {
         buttonDown = true;
     }
 
     public void ButtonIsUp()
     {
         buttonDown = false;
     }
     
     public void SwitchDirection()
     {
          moveRight = !moveRight;
     }
     
     void Update()
     {
         if (buttonDown && moveRight)
         {
             transform.Translate(Vector3.right * Time.deltaTime * speed);         
         } else if (buttonDown) {
             transform.Translate(Vector3.left * Time.deltaTime * speed);
         }
     }

Then add a UI Button, you can get rid of the image and text part or put a canvas group and set Alpha to 0 (either way it'll make it transparent).

Add an event trigger to it and add 2 new event types, one for Pointer Down and one for Pointer Up.

In Pointer Down click + twice and drag the player with that script on it onto the two slots that appear. Then from the drop down select YourScriptName -> ButtonIsDown in one and YourScriptName -> SwitchDirection in the other.

In Pointer Up click + once and drag the play (with the script) onto the slot. From the drop down select YourScriptName -> ButtonIsUp.

You can test this in the editor as the UI accepts either touch or mouse clicks. You'll need your speed variable in there as well.

Comment
Add comment · Show 4 · 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 Michael Yu · Jun 12, 2016 at 08:11 AM 0
Share

@$$anonymous$$mmpies Ok, I think I understand. I will mark your answer as the solution for now, and I will try it later because I have some work to do right now. But from the looks of it, the code you posted seems like it will work. Thanks!

avatar image Michael Yu · Jun 12, 2016 at 07:59 PM 0
Share

So, I tested out your script, but I am stuck at the event trigger part. It wont let me drag the character to the event type, and what do you mean by "play(with the script)". Thanks!

avatar image Mmmpies Michael Yu · Jun 12, 2016 at 08:20 PM 0
Share

Typo - I meant Player (with the script)

So put the script on the Player (your character) and then drag your character onto the slot.

avatar image Michael Yu · Jun 12, 2016 at 08:11 PM 1
Share

never$$anonymous$$d I found it thanks!

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

180 People are following this question.

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

Related Questions

Player Dips Into Ground After Jump 0 Answers

C#: How do you make a 2D game object jump? 1 Answer

Checking if the player jumps while not grounded 0 Answers

Making a high score script 0 Answers

Having a couple of problems regarding 2D Movement. 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