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 /
avatar image
1
Question by S_Byrnes · Jan 11, 2016 at 05:36 AM · c#inputtouchjumping

C# Screen.width Touch Input In Multiple Scripts Affect Each Other - Please Help

Hi guys, I'm have a small problem with my touch input code for mobile devices. What I have is a character that jumps when you touch the left of the screen and shoots his gun when you touch the right of the screen.

At the moment this is working fine, but I have these in separate scripts, so when I press the jump side, the shoot side is picking up the input and ends up changing the direction that he was aiming.

This is the left side's script (player.cs):

 foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
             {
                 isJumping = true;
             }
         }

This is the right side's script (shoot.cs):

 foreach (Touch touch in Input.touches)
         {
             if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width/2)
             {
                 shootActive = true;
             }
         }

Is there anything you notice that I'm not doing or something? I'd really appreciate the help, thanks a lot!

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

Answer by L_Artista · Jan 11, 2016 at 06:29 AM

It will be better if they are in one script

             Shoot shootScript;
     foreach (Touch touch in Input.touches)
     {
         if (touch.phase == TouchPhase.Began)
         {
             if(touch.position.x < Screen.width / 2)
             {
                 isJumping = true;
             }
             else if(touch.position.x > Screen.width/2)
             {
                 shootScript.shootActive = true; //
             }

         }
     }
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 S_Byrnes · Jan 11, 2016 at 06:53 AM 0
Share

Hey thanks for that I really appreciate it! I didn't know you could reference other scripts when using Input's.

I've tried it though, and it's still having that issue. Furthermore it's not shooting the projectile when touching the right of the screen anymore, it's only ai$$anonymous$$g the gun in the correct location.

Any other ideas?

avatar image S_Byrnes · Jan 11, 2016 at 07:59 AM 0
Share

I just figured out why it wouldn't shoot, it's because I have the shoot script linked to a separate object, so I replaced your shootScript.shootActive = true; with GameObject.FindGameObjectWithTag("Cannon").GetComponent<Shoot>().mouseDown = true;.

And I realize why I'm getting the issue with the ai$$anonymous$$g, it's actually because it thinks the ai$$anonymous$$g is a mouse location, and not a touch location, it was in a completely different script that I forgot about. It's in JavaScript though because I couldn't find a method that worked in C#.

Do you know how I could replace the mouse position with touch position ins$$anonymous$$d?

 function Update ()
 {
     mouse_pos = Input.mousePosition;
     mouse_pos.z = 5.23; //The distance between the camera and object
     object_pos = Camera.main.WorldToScreenPoint(target.position);
     mouse_pos.x = mouse_pos.x - object_pos.x;
     mouse_pos.y = mouse_pos.y - object_pos.y;
     angle = $$anonymous$$athf.Atan2(mouse_pos.y, mouse_pos.x) * $$anonymous$$athf.Rad2Deg;
     angle = $$anonymous$$athf.Clamp(angle, -22, 40);
     transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
 }
avatar image S_Byrnes · Jan 11, 2016 at 08:14 AM 0
Share

Alright never$$anonymous$$d, I solved the rest of my issues, lol.

For reference replaced mouse_pos = Input.mousePosition; with:

 for (var touch: Touch in Input.touches) {
 
 if (touch.position.x > Screen.width / 2){
 
 mouse_pos = Input.touches[0].position;

$$anonymous$$arking your answer as correct, thanks for the help!

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

58 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 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

Gravity Switch Touch 1 Answer

Mobile game Player follow finger input problem (C#) 2 Answers

Convert Touch input to Mouse input C#? 1 Answer

How to convert Touch Input To Mouse Input C# unity? 1 Answer

Input.touchSupported doesn't exist? 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