- Home /
Android OnMouseDown character movement
Hello,I started making an android game with unity and Im kind of stuck at making the character move.Im using GUI Textures to move the character.For example,to move right I use this script which I attach to the GUI Texture placed on the right of the screen :
var Player : Transform;
var CharacterSpeed = 5.0;
function OnMouseDown () {
Player.transform.Translate(Vector3.right * CharacterSpeed * Time.deltaTime);
}
The problem is that it only pushes the object a little bit.What I want is a smooth movement. Im new in unity and in scripting.I also am not familiar with ontouch fazes. Is there any way to do it with the OnMouseDown function ?
Thank you.
Edit : Oh,and if I change the script into this :
var Player : Transform;
var CharacterSpeed = 5.0f;
function Update () {
OnMouseDown();
}
function OnMouseDown () {
Player.transform.Translate(Vector2.right * CharacterSpeed * Time.deltaTime);
}
Then the character moves constantly.
Edit 2 :
When I changed the OnMouseDown to OnMouseOver and I click on it(on my android phone) the character/object won't stop. P.s. I noticed that if I click on the GUITexture then click somewhere else on the screen then the player stops.I assume that's because the OnMouseOver is not being called anymore.
Answer by zharik86 · Jun 24, 2014 at 07:38 PM
For mobile device, such as android best way using touch event. I wrote small example with comment(write on CSharp):
public Transform myPlayer = null;
public float myCharacterSpeed = 5.0f;
private bool isTou = false; //checks touch for our texture
private int isFingTou = -1; //which finger contact with screen
void LateUpdate() {
if(Input.touchCount > 0) { //count touches
for (int i = 0; i < Input.touchCount; i++) { //see every touch
//TouchPhase.Began - Works only once in case of a screen contact
if (Input.GetTouch(i).phase == TouchPhase.Began && !isTou) { // finger touch screen
if (this.guiTexture.HitTest(Input.GetTouch(i).position)) { //Touch texture
isTou = true;
isFingTou = Input.GetTouch(i).fingerId;
}
}
if (isTou) {
this.myMovePlayer();
}
//End of touch
if (Input.GetTouch(i).phase == TouchPhase.Ended && Input.GetTouch(i).fingerId == isFingTou) {
isTou = false;
isFingTou = -1;
}
}
}
}
public void myMovePlayer() {
myPlayer.Translate(Vector2.right * myCharacterSpeed * Time.deltaTime);
}
And attach this script at your guiTexture. I hope it to you will help.
Thank you soo much,it works just the way I want it to.
BTW,on line 17 - getTouch should be GetTouch.
Answer by robertbu · Jun 22, 2014 at 06:22 PM
Try changing OnMouseDown() to OnMouseOver(). OnMouseDown() just gets called one time when the mouse click or the finger touch is first detected.
P.S. You don't want your edit code.
This has been bothering me for days,I swear. It was realy annoying.I can't thank you enough for this simple and super fast answer!
$$anonymous$$hm,just one more thing.When I changed the On$$anonymous$$ouseDown to On$$anonymous$$ouseOver and I click on it(on my android phone) the character/object won't stop. P.s. I noticed that if I click on the GUITexture then click somewhere else on the screen then the player stops.I assume that's because the On$$anonymous$$ouseOver is not being called anymore.
Your answer
Follow this Question
Related Questions
How to create random movement in 2D 2 Answers
I Need Help With Mobile Touch Movement. 0 Answers
Move horizontally on touch 0 Answers
Android 2d movement/waypoint script issues 0 Answers
Linear laggy/stuttery movement 1 Answer