- Home /
No matter what button all methods are called.
No matter which button I press all methods are called void Update(){
if (Input.GetMouseButton (0)){
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
if(hit.collider != null){
Move.Right();
Debug.Log("right button");
}
Heres my script with void right
public void Right (){
GetComponent<Rigidbody2D>().velocity = RightForce;
//GetComponent<Rigidbody2D>().AddForce(RightForce);
Debug.Log ("moveScript was called succesfully");
if(spriterenderer.sprite == sprite2){
spriterenderer.sprite = sprite1;
}
}
public void Left(){
GetComponent<Rigidbody2D> ().AddForce (leftforce);
if(spriterenderer.sprite == sprite1){
spriterenderer.sprite = sprite2;
}
}
public void Jump(){
if (grounded) {
GetComponent<Rigidbody2D> ().AddForce (Jumpforce);
}
}
public void Shoot(){
//Instantiate(bullets
}
}
Heres my script for the left button
void Update () {
if (Input.GetMouseButton (0)) {
///Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//RaycastHit hit;
//if (Physics.Raycast (ray, out hit) && hit.collider.gameObject.name == "MoveLeft") {
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if( hit.collider != null){
Move.Left ();
}
I also want this to be mobile compatible is this possible. Also I should mention I don't even have to touch near the buttons.
Answer by Hexer · Jul 27, 2015 at 01:04 AM
The correct way of calling right button for the pc is Input.GetMouseButton(1).
Because you set both mouseclicks to 0. (The left button) you will indeed call all methods. So change the right button to (1), until you know how to make easy touch input with OnMouseDown() or by assigning the region with combination of Input.GetMouseButton(0)
If you want it to be mobile and dont want to make use of buttons (if you do check void OnMouseDown()) You could divide the screen and assign the regions as individual touch regions.
Here a link to a similar question about assigning those regions : http://answers.unity3d.com/questions/143918/touching-left-or-right-side-of-iphone-screen.html
Sorry I didn't explain well enough it's on screen buttons sorry about that and it's a platformer and holding on mouse down does nothing or very little so I need to be able to hold it.
when On$$anonymous$$ouseDown() just use InvokeRepeating("name",0,60.0f); to call another function multiple times, in this line of code that is 60x a second. And when On$$anonymous$$ouseUp() you CancelInvoke("name") the function. Which makes it stop running, until it gets Invoked again by the On$$anonymous$$ouseDown()
It is a pretty handy work around for easy buttons that has to be held for an amount of time.
(Im just re-engineering the code in my head, in theory it should work but because I have not tested it yet Im not sure if it would work correctly without any shenanigans)
On$$anonymous$$ouseDown only works when you attach a collider to it and click in the green area of the collider. So you make seperate sprites with separate colliders. for each button. 1 button for left and 1 for right.
And yes you call the method/function with InvokeRepeating. In your case it should be repeating "void Right" or "void Left"
void On$$anonymous$$ouseDown(){ InvokeRepeating("Left",0,60.0f);}
void On$$anonymous$$ouseUp(){CancelInvoke("Left");}
void Left(){
//your movement logic
}
I headed back behind my trusty pc ins$$anonymous$$d of writing inside of my ipad. AND I FIXED THE PROBLE$$anonymous$$.
using UnityEngine;
using System.Collections;
public class ButtonPressed : $$anonymous$$onoBehaviour {
bool X = false;
// Use this for initialization
void On$$anonymous$$ouseDown(){
X = true;
}
void On$$anonymous$$ouseUp(){
//CancelInvoke ("left");
X = false;
}
void left(){
//$$anonymous$$ovementLogic
}
void Update(){
if (X == true) {
//Call the function 60x per second (somehow the same as 60 frames per second)
InvokeRepeating ("left", 0.1f, 60.0f);
} else {
CancelInvoke("left");
}
}
}
I used a combination of bool to check if X was true or not. If it was then it would be invoking the function from the Update(). On $$anonymous$$ouse Up the X would turn false and would go to the else in the Update() making it Cancel the Invoke
You have to play around with the position.z in the inspector. Set the Floor to a lower or higher .Z value (or do it for the buttons) That should do the trick. The collider of the floor should not interfere with the buttons anymore,after this is done.
(because I set my main camera a little bit weird, I had to change the z value of the floor to a positive number.)