Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Lylkai · Sep 04, 2015 at 09:48 PM · movementmousemovement scripttransform.positioninput.getaxis

How can I use both mouse and keyboard to do the same thing? [SOLVED]

Hello!

I've just finished the Breakout Game tutorial and I want to move my paddle object with both keyboard and mouse. I want the player to choose which one he/she wants.

 public float paddleSpeed = 1f;

 private Vector3 playerPos = new Vector3 (0, -9.9f, 0);

 // Update is called once per frame
 void Update () {

     float xPos = transform.position.x + (Input.GetAxis ("Mouse X") * paddleSpeed);
     playerPos = new Vector3 (Mathf.Clamp (xPos, -7.5f, 7.5f), -9.9f, 0f);
     transform.position = playerPos;

     }
 
 }

I've discovered how to move with my mouse but how can I add 'Input.GetAxis ("Horizontal")' into the script? And how can I hide my mouse pointer?

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

Answer by Positive7 · Sep 04, 2015 at 10:58 PM

 using UnityEngine;
 public class asd : MonoBehaviour
 {
 
     public float paddleSpeed = 1f;
     private Vector3 playerPos = new Vector3(0, -9.9f, 0);
     bool mouseMoves;
     void Start() {
         Cursor.visible = false;
     }
     void Update()
     {
 
         float m = Input.GetAxis("Mouse X");
         float h = Input.GetAxis("Horizontal");  
         float[] t = {m,h};                      
         for (int i = 0; i < t.Length; i++) {
             if (t[0] != 0)
             {
                 mouseMoves = true;
                 float xPos = transform.position.x + t[0] * paddleSpeed;
                 playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
                 transform.position = playerPos;
             }
             
             else if(t[1] != 0 && !mouseMoves)
             {
                 float xPos = transform.position.x + t[1] * paddleSpeed;
                 playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
                 transform.position = playerPos;
             }
             else
             {
                 mouseMoves = false;
             }
         } 
     }
 }

or Simply :

 using UnityEngine;
 public class asd : MonoBehaviour
 {
 
     public float paddleSpeed = 1f;
     private Vector3 playerPos = new Vector3(0, -9.9f, 0);
     bool mouseMoves;
     void Start()
     {
         Cursor.visible = false;
     }
     void Update()
     {
 
         if (Input.GetAxis("Mouse X") != 0)
         {
             mouseMoves = true;
             float xPos = transform.position.x + Input.GetAxis("Mouse X") * paddleSpeed;
             playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
             transform.position = playerPos;
         }
 
         else if (Input.GetAxis("Horizontal") != 0 && !mouseMoves)
         {
             float xPos = transform.position.x + Input.GetAxis("Horizontal") * paddleSpeed;
             playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9.9f, 0f);
             transform.position = playerPos;
         }
         else
         {
             mouseMoves = false;
 
         }
     }
 }
Comment
Add comment · Show 3 · 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 Lylkai · Sep 04, 2015 at 11:52 PM 0
Share

$$anonymous$$an thanks a lot! This is what I was looking for but now my object starts at the middle of the screen. I want it to start at Vector3 (0, -9.9f, 0) and move from there in X-Axis. How can I do that?

[UPDATE]

Never$$anonymous$$d. I just figured it out:

 void Start()
     {
         Cursor.visible = false;
         transform.position = new Vector3(0, -9.9f, 0);
     }

Thank you!

avatar image Positive7 · Sep 05, 2015 at 12:07 AM 1
Share
 void Start()
     {
         transform.position = playerPos;
         Cursor.visible = false;
 
     }
avatar image vessago · Aug 10, 2018 at 07:11 AM 0
Share

Wow, thanks, m8. This helped and learned me a lot. Here is your adjusted code for 2D movement:

  private void $$anonymous$$anage$$anonymous$$ovement() {
             
             float mX = Input.GetAxis("$$anonymous$$ouse X");
             float h = Input.GetAxis("Horizontal");
             float mY = Input.GetAxis("$$anonymous$$ouse Y");
             float v = Input.GetAxis("Vertical");
             float[] t = { mX, h, mY,v };
             for (int i = 0; i < t.Length; i++) {
                 if (t[0] != 0 || t[2] != 0) {
                     mouse$$anonymous$$oves = true;
                     float xPos = transform.position.x + t[0] * $$anonymous$$ovementSpeed;
                     float yPos = transform.position.y + t[2] * $$anonymous$$ovementSpeed;
                     playerPos = new Vector3($$anonymous$$athf.Clamp(xPos, $$anonymous$$usX$$anonymous$$ovementConstrait, plusX$$anonymous$$ovementConstrait),
                         $$anonymous$$athf.Clamp(yPos,$$anonymous$$usY$$anonymous$$ovementConstrait, plusY$$anonymous$$ovementConstrait));
                     transform.position = playerPos;
                 } else if ((t[1] != 0 || t[3] != 0) && !mouse$$anonymous$$oves) {
                     float xPos = transform.position.x + t[1] * $$anonymous$$ovementSpeed;
                     float yPos = transform.position.y + t[3] * $$anonymous$$ovementSpeed;
                     playerPos = new Vector3($$anonymous$$athf.Clamp(xPos, $$anonymous$$usX$$anonymous$$ovementConstrait, plusX$$anonymous$$ovementConstrait),
                         $$anonymous$$athf.Clamp(yPos,$$anonymous$$usY$$anonymous$$ovementConstrait,plusY$$anonymous$$ovementConstrait));
                     transform.position = playerPos;
                 } else {
                     mouse$$anonymous$$oves = false;
                 }
             }
         }

I dont even know how comes that arrow keys are registered for movement. Its some default thing?

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

29 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 avatar image avatar image avatar image

Related Questions

Input.GetAxis going crazy when I click outside the game window 0 Answers

Can someone help me rewriting my script from transform.position to transform.Translate? 1 Answer

Movement scrip 0 Answers

How to avoid jittering / stuttering when colliding with objects in 3D? 2 Answers

Moving walls in multiple places 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