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 Pulkit30 · Aug 02, 2018 at 03:02 PM · scripting problemuibuttonsplay

Need help In creating android game with UI buttons ?Please Help

alt text

I have made these buttons to make this game's controls compatible for android and below is my playermovement script I want to make my UI buttons in screen such that when I use buttons it feels like controlling through two keys of key board

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityStandardAssets.CrossPlatformInput;
 
 
 public class PlayerMovement : MonoBehaviour {
 
     //This is a reference to the Rigidbody component called "rb"
     public Rigidbody rb;
 
     
     public float forwardforce = 200f;
     public float sidewaysforce = 500f;
     public float upwardforce = 600f;
     public int Start()
     {
         Scene currentScene = SceneManager.GetActiveScene();
         return currentScene.buildIndex;
     }
     
 
 
 
     // We marked this as "Fixed Update"because we are using it to mess with physics
     public void FixedUpdate() {
         rb.AddForce(0, 0, forwardforce * Time.deltaTime);    //Add a forward force     
        
         if (Input.GetKey("d"))
         {
             
             Debug.Log("Right");
             Right();
            
         }
 
         if (Input.GetKey("a"))
         {
            
             Debug.Log("Left");
             Left();
         }
         if (Start() == 5)
         {
             if (Input.GetKey("w"))
             {
                 rb.AddForce(0, upwardforce * Time.deltaTime, 0, ForceMode.VelocityChange);
             }
         }
         if (rb.position.y < -1f)
         {
             FindObjectOfType<GameManager>().EndGame();
         }
 
     }
 
 
     public void Right()
     {
         rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
     public void Left()
     {
         rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
 }

Please help me and thanks in advance to whoever helps

screenshot-1.png (94.1 kB)
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 MT369MT · Aug 02, 2018 at 05:18 PM

Hi, create a new script and name it TouchControl and copy-paste this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 
 public class TouchControl : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
 {
     public bool Pressed = false;
 
     public UnityEvent onPressed;
 
     public void OnPointerDown(PointerEventData eventData)
     {
         Pressed = true;
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         Pressed = false;
     }
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         Pressed = true;
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         Pressed = false;
     }
 
     public void Update()
     {
         if (Pressed)
         {
             onPressed.Invoke();
         }
     }
 }



Now add this script to both your buttons and in the inspector there will appear a "On Pressed ()" function with written "List is Empty". Click the + button and there will appear a "None (Object)" field. Drag and drop your player from your Hierarchy and then click on the "No Function" bar, choose Player Movement and then choose your Right () / Left () function.

Ask if you don't understand or you need something else.

Comment
Add comment · Show 5 · 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 Pulkit30 · Aug 03, 2018 at 06:47 AM 0
Share

Hello Thanks for the Help but I am having one more problem when use your script of TouchControl . It will keep on updating the last button pressed . if I run and level restart it automatically goes to the direction I last pressed .

avatar image MT369MT · Aug 03, 2018 at 04:08 PM 0
Share

How do you restart your scene? with Scene$$anonymous$$anager.LoadScene?

avatar image Pulkit30 MT369MT · Aug 04, 2018 at 04:33 AM 0
Share

Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name) this is the line I use to restart the same scene

avatar image MT369MT Pulkit30 · Aug 04, 2018 at 09:03 AM 0
Share

How is it possible that if you restart the scene it will go to the last pressed direction? If there isn’t something static it shouldn’t do it. Anyway I tried to recreate it in Unity and it works fine for me.

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

200 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to create a lasting UI button that would set a game object to active . 0 Answers

Please help! After build on Android UI buttons can't enable/disable script but works when using Unity remote. 0 Answers

UI Buttons Stop Working After I Load Another Scene And Then Come Back. 4 Answers

Value chages when button is held down, but after release it reverts to old value. 0 Answers

Changing Color of Material with Bool and Unity UI 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