Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 momocon · Jan 20, 2014 at 09:41 PM · objectmobiletouchdragdragging

How Can I Drag the Object With Touch ? (Mobile)

Hello,

I'm making a pong game for iPhone and I'm stuck at one point and in need of assistance! I want to drag the Player Object just by touching on IT, not anywhere else. I can move it by clicking anywhere on the screen, but that makes it impossible to implement 2-player game. I have 2 individual player sprites and I want them both can be controlled by dragging separately on the same scene.

Thank you!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Exalia · Jan 20, 2014 at 10:42 PM

Hi there, I made a game today myself using this script that is similar your pong game.

 using UnityEngine;
 using System.Collections;
 
 //Class to control rackets via touch
 public class TouchControl : MonoBehaviour 
 {
     //Public Variables
     public GameObject player1;
     public GameObject player2;
     //A modifier which affects the rackets speed
     public float speed;
     //Fraction defined by user that will limit the touch area
     public int frac;
 
     //Private Variables
     private float fracScreenWidth;
     private float widthMinusFrac;
     private Vector2 touchCache;
     private Vector3 player1Pos;
     private Vector3 player2Pos;
     private bool touched = false;
     private int screenHeight;
     private int screenWidth;
     // Use this for initialization
     void Start () 
     {
         //Cache called function variables
         screenHeight = Screen.height;
         screenWidth = Screen.width;
         fracScreenWidth = screenWidth / frac;
         widthMinusFrac = screenWidth - fracScreenWidth;
         player1Pos = player1.transform.position;
         player2Pos = player2.transform.position;
     }
     
     // Update is called once per frame
     void Update () 
     {
 //If running game in editor
 #if UNITY_EDITOR
         //If mouse button 0 is down
         if(Input.GetMouseButton(0))
         {
             //Cache mouse position
             Vector2 mouseCache = Input.mousePosition;
             //If mouse x position is less than or equal to a fraction of the screen width
             if (mouseCache.x <= fracScreenWidth)
             {
                 player1Pos = new Vector3(-7.5f, 0.5f, Mathf.Clamp(mouseCache.y / screenHeight * speed, 0, 8));
             }
             //If mouse x position is greater than or equal to a fraction of the screen width
             if(mouseCache.x >= widthMinusFrac)
             {
                 player2Pos = new Vector3(7.5f, 0.5f, Mathf.Clamp(mouseCache.y / screenHeight * speed, 0, 8));
             }
             //Set touched to true to allow transformation
             touched = true;
         }
 #endif
         //If a touch is detected
         if (Input.touchCount >= 1)
         {
             //For each touch
             foreach (Touch touch in Input.touches)
             {
                 //Cache touch position
                 touchCache = touch.position;
                 //If touch x position is less than or equal to a fraction of the screen width
                 if (touchCache.x <= fracScreenWidth)
                 {
                     player1Pos = new Vector3(-7.5f, 0.5f, Mathf.Clamp(touchCache.y / screenHeight * 8, 0, 8));
                 }
                 //If mouse x position is greater than or equal to a fraction of the screen width
                 if(touchCache.x >= widthMinusFrac)
                 {
                     player2Pos = new Vector3(7.5f, 0.5f, Mathf.Clamp(touchCache.y / screenHeight * 8, 0, 8));
                 }
             }
             touched = true;
         }
     }
 
     //FixedUpdate is called once per fixed time step
     void FixedUpdate()
     {
         if (touched)
         {
             //Transform rackets
             player1.transform.position = player1Pos;
             player2.transform.position = player2Pos;
             touched = false;
         }
     }
 }
 

I'm currently using this script so I know it works. Let me know if you use it and run into issues

Comment
Add comment · Show 11 · 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 momocon · Jan 20, 2014 at 10:47 PM 0
Share

I get the point you want to make, but there is a little problem about it. I want to move the object, by only dragging the object itself, not a whole area. Is there any way to make this happen without splitting the screen or something ?

Thank you for your response!

avatar image momocon · Jan 20, 2014 at 10:55 PM 0
Share

I get the point you want to make, but there is a little problem about it. I want to move the object, by only dragging the object itself, not a whole area. Is there any way to make this happen without splitting the screen or something ?

Thank you for your response!

avatar image Exalia · Jan 20, 2014 at 10:57 PM 0
Share

Sure I'll try again haha

This method requires you to set up a collider on your pong racket thingies :)

 foreach(Touch touch in touches)
 {
  Ray ray = Camera.main.ScreenPointToRay(Input.touch.position);
  RaycastHit hit;
 
  if(Physics.Raycast(ray, out hit, 100))
  {
  //$$anonymous$$ight need to set X and Z depending on how your game is set up as touch.position is a 2D Vector
   sprite.transform.position = touch.position;
  }
 }

What's happening here is that a Ray is cast from the camera whenever a touch is made, if the Ray collides with something within 100 units (Z units I believe) it will set the sprite position to that position.

You might want to do a check to make sure your ray collided with your pong sprite or something

avatar image momocon · Jan 20, 2014 at 11:19 PM 0
Share

When I use this code, it gives an error like this. " The name 'touches' does not exist in the current context.

EDIT: Btw here's a screenshot of my object's properties.

alt text

screen shot 2014-01-21 at 01.20.45.png (453.7 kB)
avatar image Exalia · Jan 20, 2014 at 11:25 PM 0
Share

This should fix your error

 if (Input.touchCount >= 1)
 {
  foreach(Touch touch in Input.touches)
  {
   Ray ray = Camera.main.ScreenPointToRay(touch.position);
   RaycastHit hit;
   if(Physics.Raycast(ray, out hit, 100))
  }
 }
 
Show more comments
avatar image
1

Answer by robertbu · Jan 20, 2014 at 11:01 PM

Here is one solution that uses the OnMouse*() callbacks. It will work 'as is' for mobile, but it is not the most efficient method.

http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html

There are lots of posts with drag and drop scripts. Most use Raycasting and the mouse. It is pretty straightforward to convert from mouse to touch. You will find a mouse solution here:

http://answers.unity3d.com/questions/498396/how-to-click-and-drag-an-object-at-full-speed.html

Comment
Add comment · Show 1 · 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 Exalia · Jan 20, 2014 at 11:52 PM 0
Share

momocon take a look at the links posted here

avatar image
0

Answer by LostInCode404 · Jun 29, 2014 at 09:08 AM

I've recently made a tutorial on touch and drag objects on my blog that may help you for your purpose. link - http://newtonians3d.blogspot.in/2014/06/a-simple-and-efficient-touch-and-drag.html I hope this was helpful.

Comment
Add comment · 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
0

Answer by sycr99 · Nov 11, 2020 at 10:02 AM

@Exalia Hello .. I have some objects moving in a grid. I would be glad if you could help me how I need to write code to move these objects only left, right and forward on the phone.

Comment
Add comment · 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

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

23 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

Related Questions

Dragging UI Image by touch 3 Answers

Mobile touch dragging from mouse dragging 1 Answer

Finger gesture works/fails on different devices? 0 Answers

Rotating a gameobject via dragging finger 1 Answer

Box Drag and throw away script for mobile? 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