Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by SUPPEAR · Aug 13, 2015 at 09:07 PM · c#movementbuttonmobile

Character only moves a little when button is clicked.

Hello. I am making this mobile game and I have just started to get the character to move. I wanted to make the script so that when the button is clicked and held the character would move unless the button is no longer held. When I tested the script out the character only moves a little bit to the left instead of constantly moving to the left.

 using UnityEngine;
 using System.Collections;
 
 public class Move : MonoBehaviour
 {
     public float speed = 5f;
     
     public void OnMouseDown ()
     
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
 
 }

All help is appreciated.

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
1
Best Answer

Answer by CJThomas · Aug 14, 2015 at 04:41 AM

OnMouseDown() only fires once when the mouse is pressed. You should try something like this.

  using UnityEngine;
  using System.Collections;
  
  public class Move : MonoBehaviour
  {
      public float speed = 5f;
      private bool mounseDown = false;
      public void OnMouseDown ()
      {
         mouseDown = true;
      }
      public void OnMouseUp()
      {
         mouseDown = false;
      }
 
      public void Update()
      {
         if(mouseDown){
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
      }
 }
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 SUPPEAR · Aug 14, 2015 at 05:17 AM 0
Share

Oh thanks! I had no idea it only fires once so that will help me a lot!

avatar image
0

Answer by K-Rex · Aug 14, 2015 at 06:06 AM

Instead of using the "OnMouseDown" function/void (whatever you wanna call it) use "FixedUpdate". This is used commonly when moving gameobjects or characters. It's like the "Update" function but it will only be called 50 times per second so your character doesn't move faster if you are getting more frames per second. Now. For the actual question, inside the "FixedUpdate" function, say

if (Input.GetKey(KeyCode.Mouse0)) { transform.position += Vector3.left * speed * Time.deltaTime; }

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

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

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

[C#] Both button check with delay. 1 Answer

How can i do: When the button "play"is pressed, the player can start to move, but if not pressed the player cannot move. 1 Answer

how to make rail and then make something move on it? 0 Answers

Change the position of a UI component through pressing a UI Button 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