Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TheNucleaLion · Aug 19, 2020 at 07:31 PM · 2d gameandroid buildmovement scriptbuttonstouch controls

Touchscreen Movement on buttons

I am building a game on android (clone of space invaders 2D). So the ship has to move only left and right in 2D. I don't want to use a joystick, I want buttons for better phone experience. This is my moving algorithm for the same game, PC version. Please help, I couldn't find anything useful on internet that works for me. How can I implement touchscreen button control on this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spaceship : MonoBehaviour
 {
     public float speed = 30;
 
     public GameObject theBullet;
 
     void FixedUpdate()
     {
         float horzMove = Input.GetAxisRaw("Horizontal");
 
         GetComponent<Rigidbody2D>().velocity = new Vector2(horzMove, 0) * speed; 
     }
 
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetButtonDown("SpaceShipShoot"))
         {
             Instantiate(theBullet, transform.position, Quaternion.identity);
 
             soundManager.Instance.playOneShot(soundManager.Instance.BulletFire);
         }
     }
 }
 
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
0
Best Answer

Answer by Llama_w_2Ls · Aug 19, 2020 at 07:53 PM

Well first of all, i wouldnt want to be getting the Rigidbody2D every frame as thats quite unnecessary and not very CPU mobile friendly. Secondly, you can have two public functions, one for moving left and right, which happen on button press. For example:

     Rigidbody2D rb2D;
     public float speed = 30f;
 
     private void Start()
     {
         rb2D = GetComponent<Rigidbody2D>();
     }
 
     public void MoveLeft()
     {
         rb2D.velocity = new Vector2(-1, 0) * speed;
     }
 
     public void MoveRight()
     {
         rb2D.velocity = new Vector2(1, 0) * speed;
     }

@TheNucleaLion (im assuming youve worked with UI Buttons before, right?)

Comment
Add comment · Show 7 · 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 TheNucleaLion · Aug 19, 2020 at 07:58 PM 0
Share

@Llama_w_2Ls yes, I did work. I mostly used them for menus(pause, main menu, death scene etc) and other UI. I will try right now your algorithm and will answer after that

avatar image TheNucleaLion · Aug 19, 2020 at 08:05 PM 0
Share

@Llama_w_2Ls I also got a problem with the unity placement part. Should I attach the spaceship in the same canvas as the buttons?

avatar image Llama_w_2Ls TheNucleaLion · Aug 19, 2020 at 08:08 PM 0
Share

No, you dont have to do that, but on the UI button, you need to reference the script that the spaceship contains, and reference it inside of the onValueChanged() section of the button in the inspector, then search for the right function in the dropdown. Theres a few tutorials on UI scripting available on youtube

avatar image TheNucleaLion Llama_w_2Ls · Aug 19, 2020 at 08:14 PM 0
Share

@Llama_w_2Ls okk, got this, now it's working. I still got only one more issue: after releasing the button, the spaceship doesn't stop, it keeps going left/right until the other button is pressed. How can I fix it?

Show more comments

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

175 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

Related Questions

Buttons touchscreen movement bug 1 Answer

Touch controls no longer work on movement script,2D movement script not working with mobile touch 0 Answers

can i get the code for android touch controls to move the object slide along y direction in 2D 1 Answer

Make touch buttons transparent 2 Answers

How to make slippery/smooth movement in Unity 2D? 3 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