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
0
Question by Spaldin · Aug 04, 2015 at 08:26 PM · c#2dmovementbutton

Unity 2D - Basic Button Movement

I already made moving object with arrows but now I want to make arrows on the screen and make player move when arrow is clicked.

So I started with this:

 void Update() {
     if (Input.GetMouseButtonDown(0))
         Debug.Log("Pressed left click.");
 }

So the problem is I can't use same script as for arrows and here is why:

 void FixedUpdate () {

 grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 anim.SetBool ("Ground", grounded);

 anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);


 float move = Input.GetAxis ("Horizontal");

 anim.SetFloat ("Speed", Mathf.Abs(move));

 GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

 if (move > 0 && !facingRight)
     Flip ();
 else if (move < 0 && facingRight)
     Flip ();

  }

In this code there is no declaration of any buttons clicked and I don't understand anything. Can someone please write script for basic movement of object?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Firedan1176 · Aug 04, 2015 at 09:53 PM

If you're using Unity 4.6 or greater, you can create your arrow buttons on screen, and just position how you want. These button have a script called Button. At the bottom, there is a field called OnClick. Whenever you press your button in-game, this OnClick field will call whatever functions or variables you place into it, and you can do multiple! For example, if you have a public function called SendMessage() in a script on an empty gameObject in your scene, you can drag your empty gameObject into that OnClick field, select your script, and find the function 'SendMessage()' in the list, you can call that function when you press the button.

Now let's implement this:

Create a script for your buttons. Add this into your script:

 public void UseArrowButton(string button) {
 
 if(button == "w")
 //Move forward
 transform.position += transform.forward * Time.deltaTime;
 
 if(button == "a")
 transform.position += -transform.right * Time.deltaTime;
 
 if(button == "s")
 transform.position += -transform.forward * Time.deltaTime;
 
 if(button == "d")
 transform.position += transform.right * Time.deltaTime;
 
 }

Now it's untested, but you should get the idea. Now place your script on an object on your scene. You could put it on the Camera if you wanted, it doesn't really matter. Now on your buttons, add the Camera object in the OnClick field, navigate to your script, and then the function. Now there should be an empty field to the side where you can enter in a string (which is the 'button'). For each button, put in w, a, s, and d. Now if you want to use Input.GetAxis, you can just replace those.

The OnClick confused me at first, but think of it as when Unity calls Start or Update. That's all it is really doing. When you press the button, the OnClick will call a function on a script in a scene. Just make sure the function has 'public' before it.

Comment
Add comment · Show 4 · 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 Spaldin · Aug 06, 2015 at 01:59 PM 0
Share

But I'm asking for 2D and so for the script of moving in 2D

avatar image Firedan1176 · Aug 16, 2015 at 01:46 AM 0
Share

Yeah you'll need to change it, but you should get the idea.

I'm sure the point of your question is to get buttons to work with movement on the screen. Hopefully, you should be able to change the movement in my example on your own.

avatar image rutuja30 · Jul 13, 2016 at 04:55 AM 0
Share

Hello .. Thank u so much for your script, but my player object is moving very SLOWLY So please tell me how can i speed up the movements

avatar image Firedan1176 rutuja30 · Jul 30, 2016 at 07:34 PM 0
Share

Hey, sorry for the late response. You can multiply the transform.forward * Time.deltaTime with another value, say 5, to make it go faster.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Game object should move like a fish in 2D C# 1 Answer

Unusual error regarding movement in a top down 2d sidescroller 1 Answer

Sprite flip movement script not working! HELP! 0 Answers

How to make a script for "jetpack" boost animation 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