- Home /
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
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 button
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
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;
}
}
}
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;
}
}
}