Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ssiroo · Jun 22, 2014 at 06:21 PM · android2djavascriptmovement

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.

Comment
Add comment · Show 2
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 Ssiroo · Jun 23, 2014 at 02:58 PM 0
Share

This comment is a bump.

avatar image Ssiroo · Jun 24, 2014 at 01:10 PM 0
Share

Can anyone help ?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · 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 Ssiroo · Jun 30, 2014 at 07:55 PM 0
Share

Thank you soo much,it works just the way I want it to.

BTW,on line 17 - getTouch should be GetTouch.

avatar image
0

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.

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 Ssiroo · Jun 22, 2014 at 06:28 PM 0
Share

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!

avatar image Ssiroo · Jun 22, 2014 at 06:47 PM 0
Share

$$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

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

2 People are following this question.

avatar image avatar image

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


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