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 ZoomStar · Feb 19, 2014 at 11:33 PM · 2djavascriptmousemove

Mouse Click to keyCode

ok, in first step sorry for my bad en:D

So, i trying to make a 2d game, for android and i make a car, wich i need to move left and writh ....so

i made, with this code:

 var leftKey : KeyCode;
 var rightKey : KeyCode;
 
 var speed : float = 15;
 
 function Update ()
 {
     if (Input.GetKey(rightKey))
     {
         rigidbody2D.velocity.x = speed;
     }
     else if (Input.GetKey(leftKey))
     {
         rigidbody2D.velocity.x = speed *-1;
     }
     else
     {
         rigidbody2D.velocity.x = 0;
     }
 }

but i can`t move this car left and right with A and D or any buttons .....

BUT...

i need to move this car with my touch screen so, what i made it for that...i create to objects, for 1/2 of screen (left and right bottons)....

my menu work good with this code

 function OnMouseUp()

and i try and i try but i don`t know how to connect this function...becouse are two differit commponet of two differit objects.....so i stuck HERE....

it`s exist any ideea to put that fucion "OnMouseUp" to simulate a "A" or another keycode...to move my car?

Thank You:)

SORRY FOR THIS VERY BAD EN!!!

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

Answer by CodeElemental · Feb 20, 2014 at 11:30 AM

Maybe you can use

 Input.GetTouch(0).position;

and then check for whether X is on the left/right side of the screen ?

 void Update() 
 {
   if (Input.touchCount > 0)
   {
           if (Input.GetTouch(0).position.x < halfX)
           {
             // Move left.
           } else if (Input.GetTouch(0).position.x > halfX)
           {
             // Move right
           }  else 
           {
             // reset movement.
           }
   } else 
   {
     // reset movement.
   }
 }
Comment
Add comment · Show 8 · 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 ZoomStar · Feb 20, 2014 at 01:03 PM 0
Share

this code is for my car? becouse i add this code in a componet of my car and i got this error

Assets/move.cs(1,6): error CS0116: A namespace can only contain types and namespace declarations

avatar image CodeElemental · Feb 20, 2014 at 01:21 PM 0
Share

$$anonymous$$y best guess is you have some errors in the brackets. Also, (since you wrote your code snippet in javascript) this code is in C# you may want to convert it to javascript.

Find out more here

avatar image CodeElemental · Feb 20, 2014 at 01:26 PM 0
Share

From the error line number (1,6) i think you're missing

using UnityEngine;

avatar image ZoomStar · Feb 20, 2014 at 01:44 PM 0
Share

so i put this cod for my car

 function Update() 
 {
   if (Input.touchCount > 0)
   {
           if (Input.GetTouch(0).position.x < 2.5)
           {
             rigidbody2D.velocity.x = -10;
             
           } else if (Input.GetTouch(0).position.y > 2.5)
           {
             rigidbody2D.velocity.x = 10;
           }  else 
           {
            
           }
   } else 
   {
    rigidbody2D.velocity.x = 0;
   }


but my car go just in the right :( anywhere i touch my screen

avatar image CodeElemental · Feb 20, 2014 at 01:53 PM 0
Share

position The position of the touch in pixel coordinates.

from the unity Documentation. I think that halfX should be half of the screen width ( in pixels ) ex. if the width is 400 it should be 200.

also the second check is wrong.

 else if (Input.GetTouch(0).position.y > 2.5)

why check for y value? Is this desired?

Show more comments

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

20 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

Related Questions

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

2d camera help! 0 Answers

Mouse as a throttle 1 Answer

Changing material orientation dependant on mouse location? 0 Answers

What do I type so that integers interact with the animator. 0 Answers


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