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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by xxluky · Jan 24, 2015 at 09:59 AM · 2dtouch controlsmathf.clamp

Touch control for a Pong Game

Hey Guys, can you please help me with changing keybord input script to touch control?

Here is the original script (Keyboard input):

     public float paddleSpeed = 1f;
     public float xLeft;
     public float xRight;
     private Vector3 playerPos = new Vector3(0, -9.5f, 0);
 
     void Update () {
         float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
         playerPos = new Vector3 (Mathf.Clamp (xPos, xLeft, xRight), -9.5f, 0f);
         transform.position = playerPos;
     }

For now, I have this for touch control:

 public float paddleSpeed = 1f;
 public float xLeft;
 public float xRight;
 
 private Ray ray;
 private RaycastHit rayCastHit;
 
 private Vector3 playerPos = new Vector3(0, -9.5f, 0);
 
 void Update () {
 
         if (Input.GetMouseButton (0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if(Physics.Raycast(ray, out rayCastHit)){
           //Missing code, not sure how to implement Mathf.Clamp and Vector3 with Ray
    }
 }


Comment
Add comment · Show 5
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 xxluky · Jan 24, 2015 at 11:24 AM 0
Share

Any help would help, thanks...

avatar image Mmmpies · Jan 24, 2015 at 11:51 AM 0
Share

Are you using the 4.6 or above, the new UI on Click event system works for both mouse clicks and on touch devices.

If you just want a left and right buttons I can tell you how to do that right away with the new UI.

Let me know if you want a quick tutorial, or just click on the learn button and view the tutorials for the new UI.

avatar image xxluky · Jan 24, 2015 at 02:20 PM 0
Share

Hi, thanks for your reply. Not sure if i am clear with my question. The touch control works fine. The script is attached to gameobject Cube (like paddle) and it moves on the x axes. The problem is, that I need to help with the missing part of the code. I am not able to implement the first code above to the second code with the touch.

The first script is for computers and it works. I need to implement this script with the same result to the second script. The result should be controlling the paddle on the phone, not on the keyboard.

avatar image Mmmpies · Jan 24, 2015 at 03:13 PM 0
Share

I still think I'm not sure what you want, are you wanting to move in 3D space or just use left and right?

If just left and right you probably just need to use Input.mousePosition and give it a set value for Z then use Camera.main.ScreenToWorldPoint.

If that doesn't help can you post an image of your game so I can tell better what you're after.

avatar image xxluky · Jan 24, 2015 at 04:48 PM 0
Share

Well, actualy it is exactly this tutorial: Breakout game And yes, it is 2D game as my tag says, I only need to move the paddle on the X. The first script from above is the original from the tutorial. The second script is my idea, how to change the input for mobile phones using touches. I am only not able to put the original script into the second to get the same result.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mmmpies · Jan 24, 2015 at 05:21 PM

Missed the tag! :¬)

OK well I don't use 2D games myself but you can try this if you want to use ray casting:

 public void MyRaycast()
     {
         myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if(Physics.Raycast(myRay, out rayHitOut))
         {
             Debug.Log ("x = " + rayHitOut.point.x + "  y = " + rayHitOut.point.y);
             Debug.Log (Input.mousePosition);
             Debug.Log (Camera.main.ScreenToWorldPoint(Input.mousePosition));
         }
     }

That just outputs the various Debug info, you only really need the x value. As I don't use 2D I don't know what your results are likely to be but note the x value of the raycast and the x value of the mouse outputs and which, if any match up to what you expect from that tutorial.

Then we'll set the Clamp based on your results. You might want to consider the speed of the platform as well, with your method the paddle can go from one side of the screen to the other pretty fast, you might want 2 buttons like I said in the first comment we can make them act like a controller that gains value/speed if you hold it down.

Comment
Add comment · Show 9 · 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 xxluky · Jan 25, 2015 at 12:34 PM 0
Share

Is it really so complicated to take the first script and put him into the second with the rayCastHit? I am absolute beginner, but now the script is working. The paddle is moving according to mouse position. But still, a could not figure out, how to set the $$anonymous$$athf.Clamp to limit the movement with the padlle on the X axis. Now, I have this (the comments are the old script that was working with keyboard):

 using UnityEngine;
 using System.Collections;
 
 public class Paddle : $$anonymous$$onoBehaviour {
 
     public float paddleSpeed = 1f;
     public float xL;
     public float xR;
 
     private Ray ray;
     private RaycastHit rayCastHit;
 
     private Vector3 playerPos = new Vector3(0, -9.5f, 0);
 
     // Update is called once per frame
     void Update () {
 
         if (Input.Get$$anonymous$$ouseButton (0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if(Physics.Raycast(ray, out rayCastHit)){
                 Vector3 position = rayCastHit.point;
                 position.z = 0;
                 position.y = -9.5f;
                 transform.position = position; 
             }
         }
 
         //float xPos = transform.position.x + (Input.GetAxis("Horizontal") * paddleSpeed);
         //playerPos = new Vector3 ($$anonymous$$athf.Clamp (xPos, xL, xR), -9.5f, 0f);
         //transform.position = playerPos;
     }
 }
avatar image Mmmpies · Jan 25, 2015 at 02:12 PM 1
Share

Try this:

      if (Input.Get$$anonymous$$ouseButton (0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if(Physics.Raycast(ray, out rayCastHit)){
                 Vector3 position = rayCastHit.point;
 
                 float xPos = position.x;
                 playerPos = new Vector3($$anonymous$$athf.Clamp(xPos, xL, xR), -9.5f, 0f);
                 transform.position = playerPos; 
             }
         }
avatar image xxluky · Jan 25, 2015 at 02:43 PM 0
Share

Thank you for your patience. I tried your code, but it is not working. The paddle does not move at all. It even does not desplay any errors...

avatar image fafase · Jan 25, 2015 at 02:52 PM 1
Share

Did you provide a value for xL and xR in the inspector?

avatar image Mmmpies · Jan 25, 2015 at 02:54 PM 0
Share

I just used -8 for xL and 8 for xR. If you leave them at 0 then the paddles not going to move as @fafase suggested.

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

Tap vs touch controls 1 Answer

2D Animation does not start 1 Answer

Buttons touchscreen movement bug 1 Answer

Using touch as an input for 2d game. 0 Answers

Problems with Clamping Player to Screen 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