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 Ekta-Mehta-D · Apr 09, 2013 at 10:18 AM · 2d

Move object Horizontally : 2D

I am trying to move my player left and right on button click. But unfortunately it does not work.

Script i have written is here:

 if (Input.GetMouseButtonDown(0))
         {
             var hit  : RaycastHit;
             var ray  : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray,hit))
             {
                 if(hit.collider.gameObject.GetComponent("ButtonId"))
                 {
                     buttonPressed = hit.collider.gameObject.GetComponent("ButtonId");
                     if(buttonPressed.id == "RightMove")
                     {   
                         amttomove = Input.GetAxisRaw("Horizontal") * 2 * Time.deltaTime ;
                           transform.Translate(Vector3.right * amttomove);                   
                     }
                  }
               }
          }

But this code is not moving my player..

when i write below code out of Input.GetMouseButtonDown(0), it works..

 amttomove = Input.GetAxisRaw("Horizontal") * 2 * Time.deltaTime ;
                                   transform.Translate(Vector3.right * amttomove);

Where is the problem>?? pleaze any one help me to solve my problem..

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

Answer by Shankar · Apr 09, 2013 at 10:43 AM

       var upRange : float = 1.0;
      var downRange : float = 1.0;
      var waveOffset: float = 0.0;

     internal var yStartPos: float;
     internal var upPos : float;
     internal var downPos : float;

    var speed : float = 0.2;

        ===========================
 yStartPos = transform.position.y; 

       ===========================
 upPos = yStartPos +upRange;
 downPos = yStartPos - downRange;
 var weight : float = Mathf.Cos((Time.time + waveOffset) * speed * 2 * Mathf.PI) * 0.5 + 0.5;
 transform.position.y = upPos * weight + downPos * (1-weight);

      ===========================
      var leftRange : float =0.0;
       var rightRange : float =0.0;
           var offset = 0.0;
          var waveOffset: float = 0.0;

            internal var xStartPos : float;
             internal var leftPos : float;
              internal var rightPos : float;

             var speed : float = 0.2;

              ============================
     xStartPos = transform.position.x;

               ============================
              function FixedUpdate () {

 leftPos = xStartPos - leftRange;
 rightPos = xStartPos + rightRange;
 var weight = Mathf.Cos((Time.time + waveOffset) * speed * 2 * Mathf.PI) * 0.5 + 0.5;
 transform.position.x = leftPos * (1-weight) + rightPos * weight - offset;

            }


Hey this for vertical direction change. please convert this into horizontal. then u get get that. or else just change axis in transform.

this is from

http://www.lynda.com/Unity-3D-tutorials/Unity-3D-35-Essential-Training/96677-2.html

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 Ekta-Mehta-D · Apr 09, 2013 at 11:03 AM 0
Share

write in proper manner..i dont understand your code..

avatar image Shankar · Apr 09, 2013 at 11:15 AM 0
Share

alt text

see this. this is only for vertical only. u can get horizontal in the above code. sorry for my answering format.

1.png (177.0 kB)
avatar image Shankar · Apr 09, 2013 at 02:27 PM 0
Share

Can u give me some more clarification on this question. i understand that u have button in the scene. if u click that button the object goes to right. that is ok. what about left? can i take two buttons and then, can i write the code? if yes tell me i will try to write and post.

avatar image
1

Answer by IronFurball · Apr 09, 2013 at 11:27 AM

The problem is that you're using GetMouseButtonDown. GetMouseButtonDown only triggers on the moment that you're mouse clicks, which means that your player only moves for 1 frame, and then stops until you click again. If you start spam clicking the button you will probably notice that he slowly starts to move.

if you want him to move until you release the button, you will have to use GetMouseButton, so without the "Down" at the end.

Comment
Add comment · Show 5 · 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 Ekta-Mehta-D · Apr 09, 2013 at 11:52 AM 0
Share

no its not the problem.. i have tried.. :(

avatar image IronFurball · Apr 09, 2013 at 11:59 AM 0
Share

Is your raycast hitting ? You can check by writing this inside your raycaast check:

Debug.Log("The raycast hit something! yay :D");

If that doesn't show up in the console then its not hitting.

avatar image Ekta-Mehta-D · Apr 09, 2013 at 01:11 PM 0
Share

i have tested.. my input.GetButtonDown called only once.. So what do i do?

avatar image IronFurball · Apr 09, 2013 at 03:09 PM 0
Share

where did you put the Debug.Log, in the Get$$anonymous$$ouseButtonDown ? $$anonymous$$ake sure to put it in the

 if (Physics.Raycast(ray,hit))
             {}

so that you're absolutely sure your raycast is hitting something.

avatar image Ekta-Mehta-D · Apr 10, 2013 at 04:29 AM 0
Share

yes.. if(buttonPressed.id == "Right$$anonymous$$ove") { }

I have kept in this condition.. bt still not working

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

12 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

Related Questions

A node in a childnode? 1 Answer

A Question about sprites 3 Answers

2D Mouse Aiming 2 Answers

Reusing same 2D Environment in different levels 0 Answers

Need help with screen resolution and character size 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