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 bracciata · Jul 26, 2015 at 11:37 PM · c#raycastraycasthit2d

No matter what button all methods are called.

No matter which button I press all methods are called void Update(){

 if (Input.GetMouseButton (0)){
         RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
         if(hit.collider != null){
         Move.Right();
             Debug.Log("right button");
         }

 Heres my script with void right

     public void Right (){
         GetComponent<Rigidbody2D>().velocity = RightForce;
         //GetComponent<Rigidbody2D>().AddForce(RightForce);
         Debug.Log ("moveScript was called succesfully");
         if(spriterenderer.sprite == sprite2){
             spriterenderer.sprite = sprite1;
 
         }
 
     }
     public void Left(){
         GetComponent<Rigidbody2D> ().AddForce (leftforce);
                 if(spriterenderer.sprite == sprite1){
                     spriterenderer.sprite = sprite2;
                 }
     }
     public void Jump(){
         if (grounded) {
             GetComponent<Rigidbody2D> ().AddForce (Jumpforce);
         }
     }
 
     public void Shoot(){
         //Instantiate(bullets
     }
 
 }

Heres my script for the left button

 void Update () {
     if (Input.GetMouseButton (0)) {
             ///Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             //RaycastHit hit;
             //if (Physics.Raycast (ray, out hit) && hit.collider.gameObject.name == "MoveLeft") {
             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
             if( hit.collider != null){
                 Move.Left ();
             }

I also want this to be mobile compatible is this possible. Also I should mention I don't even have to touch near the buttons.

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 Hexer · Jul 27, 2015 at 01:04 AM

The correct way of calling right button for the pc is Input.GetMouseButton(1).

Because you set both mouseclicks to 0. (The left button) you will indeed call all methods. So change the right button to (1), until you know how to make easy touch input with OnMouseDown() or by assigning the region with combination of Input.GetMouseButton(0)

If you want it to be mobile and dont want to make use of buttons (if you do check void OnMouseDown()) You could divide the screen and assign the regions as individual touch regions.

Here a link to a similar question about assigning those regions : http://answers.unity3d.com/questions/143918/touching-left-or-right-side-of-iphone-screen.html

Comment
Add comment · Show 13 · 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 bracciata · Jul 27, 2015 at 01:22 AM 0
Share

Sorry I didn't explain well enough it's on screen buttons sorry about that and it's a platformer and holding on mouse down does nothing or very little so I need to be able to hold it.

avatar image Hexer · Jul 27, 2015 at 01:31 AM 1
Share

when On$$anonymous$$ouseDown() just use InvokeRepeating("name",0,60.0f); to call another function multiple times, in this line of code that is 60x a second. And when On$$anonymous$$ouseUp() you CancelInvoke("name") the function. Which makes it stop running, until it gets Invoked again by the On$$anonymous$$ouseDown()

It is a pretty handy work around for easy buttons that has to be held for an amount of time.

(Im just re-engineering the code in my head, in theory it should work but because I have not tested it yet Im not sure if it would work correctly without any shenanigans)

avatar image Hexer · Jul 27, 2015 at 01:53 AM 1
Share

On$$anonymous$$ouseDown only works when you attach a collider to it and click in the green area of the collider. So you make seperate sprites with separate colliders. for each button. 1 button for left and 1 for right.

And yes you call the method/function with InvokeRepeating. In your case it should be repeating "void Right" or "void Left"

 void On$$anonymous$$ouseDown(){ InvokeRepeating("Left",0,60.0f);}
     void On$$anonymous$$ouseUp(){CancelInvoke("Left");}


 void Left(){
 //your movement logic
 }




avatar image Hexer · Jul 27, 2015 at 02:13 AM 1
Share

I headed back behind my trusty pc ins$$anonymous$$d of writing inside of my ipad. AND I FIXED THE PROBLE$$anonymous$$.

 using UnityEngine;
 using System.Collections;
 
 public class ButtonPressed : $$anonymous$$onoBehaviour {
 
     bool X = false;
     // Use this for initialization
 
     void On$$anonymous$$ouseDown(){
 
         X = true;
     }
 
     void On$$anonymous$$ouseUp(){
 
         //CancelInvoke ("left");
         X = false;
     }
     void left(){
     //$$anonymous$$ovementLogic        
     }
 
     void Update(){
         if (X == true) {
                     //Call the function 60x per second (somehow the same as 60 frames per second)
             InvokeRepeating ("left", 0.1f, 60.0f);
         } else {
             CancelInvoke("left");
         }
     }
 }

I used a combination of bool to check if X was true or not. If it was then it would be invoking the function from the Update(). On $$anonymous$$ouse Up the X would turn false and would go to the else in the Update() making it Cancel the Invoke

avatar image Hexer · Jul 27, 2015 at 02:27 AM 1
Share

You have to play around with the position.z in the inspector. Set the Floor to a lower or higher .Z value (or do it for the buttons) That should do the trick. The collider of the floor should not interfere with the buttons anymore,after this is done.

(because I set my main camera a little bit weird, I had to change the z value of the floor to a positive number.)

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

22 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

Related Questions

Raycasting in 2D is not working 0 Answers

One 2D Raycast Being Weird While Others Work 0 Answers

Is my 2D Raycast set up correctly? because it does not seem to hit anything when i use a layer mask. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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