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 Fyriif · Apr 18, 2016 at 12:23 PM · c#androidmovementspritetouch

Using GUI buttons to move a sprite (C#)

So basically, I am making a 2 player Brick Breaker game where I have a left and right button for each player.

The problem is that I need the buttons to move a sprite (the paddle) left and right for each player, depending on the button pressed: e.g. if P1Left is pressed, then player 1's paddle is moved left.

The reason behind this is that I am making this for my computing project and its meant to me an Android App.

The paddle has a Rigidbody2D and BoxCollider added as the componenets.

Any alternative solutions are welcome

Code needs to be in C#

Thanks for your time. effort and help

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by faizidp · Apr 20, 2016 at 01:23 PM

Create a sprite for left button and another for right.... Then attach box collider2d to them. and add two separate scripts to them:
for leftButton:

 public GameObject paddle; //attach your paddle Gameobject to this
 void OnMouseDown(){
   paddle.GetComponent<Rigidbody>().addVelocity(new Vector3(-1,0,0);
 }


And another same script but with change of new(Vector3(1,0,0);
you can adjust speed in new Vector3;
I also used this in my game... You can checkout the buttonalt text alt text


in-game.png (59.8 kB)
right.png (4.4 kB)
Comment
Add comment · 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
0

Answer by aditya · Apr 20, 2016 at 01:37 PM

 bool move;
 Vector2 newPos;
 Rigidbody2D rgbd;
 
 void Start(){
     newPos = new Vector2(0f, 0f);
     rgbd = Gameobject.FindObjectWithTag("paddleP1").GetComponent<Rigidbody2D>();
 }
 void FixedUpdate(){
     if(move){
         rgbd.movePosition(rgbd.position + newPos);
     }
 }
 
 public void leftButton(bool isDown){
     if(isDown){
         move = true;
         newPos.x = -1f;
     }else{
         move = false;
     }
 }
 
 public void rightButton(bool isDown){
     if(isDown){
         move = true;
         newPos.x = 1f;
     }else{
         move = false;
     }
 }

attach this script to an empty gameObject and name it something like scriptObject, now on your both buttons add EventTrigger component and through this component add new OnPointerDown and OnPointerUp events (keep in mind to do this for both buttons of your both players), these events works same as OnClick means you had to drag and drop your scriptObject on them and then access the script on this gameObject ... for left button call leftButton method and for right button call rightButton method just make sure that when calling these methods change the isDown boolean to true in OnPointerDown and false in OnPointerUp ... and this example will work if your paddle has a tag named paddleP1 ... i think the script is self explanatory in itself

Comment
Add comment · Show 2 · 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 Fyriif · Apr 25, 2016 at 12:35 AM 0
Share

Severity Code Description Project File Line Suppression State Error CS1061 'Rigidbody2D' does not contain a definition for 'movePosition' and no extension method 'movePosition' accepting a first argument of type 'Rigidbody2D' could be found (are you missing a using directive or an assembly reference?) Brick Breaker Coursework.CSharp C:\Users\Fyriif\Desktop\Brick Breaker Coursework Game\Brick Breaker Coursework\Assets\P1$$anonymous$$ovementHandler.cs 20 Active

This is the error I'm getting with your code

Code so far (I've changed it a bit due to issues): using UnityEngine; using System.Collections;

 public class $$anonymous$$ovementHandler : $$anonymous$$onoBehaviour {
 
     bool move;
     Vector2 newPos;
     Rigidbody2D rgbd;
     public GameObject P1Paddle;
 
     void Start()
     {
         newPos = new Vector2(0f, 0f);
         rgbd = P1Paddle.GetComponent<Rigidbody2D>();
     }
     void FixedUpdate()
     {
         if (move)
         {
             rgbd.movePosition(rgbd.position + newPos);
         }
     }
 
     public void leftButton(bool isDown)
     {
         if (isDown)
         {
             move = true;
             newPos.x = -1f;
         }
         else {
             move = false;
         }
     }
 
     public void rightButton(bool isDown)
     {
         if (isDown)
         {
             move = true;
             newPos.x = 1f;
         }
         else {
             move = false;
         }
     }
 }
 
avatar image Fyriif Fyriif · Apr 25, 2016 at 01:03 AM 0
Share

Ok, so the code works now but the paddle wont move despite the added Debug Logs appearing as needed.

The paddle also wont show up in game but is visible in the editor. using UnityEngine; using System.Collections;

 public class P1$$anonymous$$ovementHandler : $$anonymous$$onoBehaviour {
     bool move;
     Vector2 newPos;
     Rigidbody2D rgbd;
     public GameObject P1Paddle;
 
     void Start()
     {
         newPos = new Vector2(0f, 0f);
         rgbd = P1Paddle.GetComponent<Rigidbody2D>();
     }
     void FixedUpdate()
     {
         if (move)
         {
             rgbd.$$anonymous$$ovePosition(rgbd.position + newPos);
         }
     }
 
     public void leftButton(bool isDown)
     {
         if (isDown)
         {
             move = true;
             newPos.x = -1f;
             Debug.Log("P1 $$anonymous$$oving Left");
         }
         else {
             move = false;
         }
     }
 
     public void rightButton(bool isDown)
     {
         if (isDown)
         {
             move = true;
             newPos.x = 1f;
             Debug.Log("P1 $$anonymous$$oving Right");
         }
         else {
             move = false;
         }
     }
 }
 

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

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

How to drag object along with dragging touch? 1 Answer

Help with iphone/android touch controls 2 Answers

How to use Android Accelerometer(Left and right tilt) to turn a sphere object and camera in that direction? 1 Answer

How to prevent touch going through UI? 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