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 Firzok · Aug 02, 2015 at 06:22 PM · unity 5touchdrag objects

How to drag an object with touch.

Hi I am making a 2D Pong style game. At first I thought that mouse simulates touch movement but it seems that is not the case here. I wrote this code to move paddle by using mouse but it does not let me use it for touch on phone. I would like to have this code edited so that I can control my paddle with touch on phone. Android phone to be precise. I am a noob at C# and unity so please don't judge me.

using UnityEngine; using System.Collections;

public class Dragger : MonoBehaviour {

 Vector3 mousePos;
 Vector3 playerPos;
 public float paddleSpeed = 1;

 
 void Update() {
     mousePos = Vector3.Lerp (Input., playerPos, Time.deltaTime * paddleSpeed);
     playerPos = new Vector3 (-22, Mathf.Clamp (mousePos.y, -12.5f, 12.5f), 0);
     gameObject.transform.position = new Vector3 (-22, playerPos.y, 0);
 }

}

P.s not all of this code is mine as I already told that I am anoob at C#

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by imilanspinka · Aug 02, 2015 at 10:31 PM

Okay, I am not going to try this since I have no Android SDKs installed, but I will try to write a code and you have to test if it works:

 using UnityEngine;
 using System.Collections;
 
 public class PaddleControl : MonoBehaviour
 {
     private Touch initialTouch = new Touch();
     private float distance = 0;
     private bool hasSwiped = false;
 
     GameObject paddle;
 
     void Start()
     {
         paddle = FindGameObjectWithName ("paddle (whatever)");
             //or FindGameObjectWithTag (...);
     }
 
     void FixedUpdate()
     {
         foreach (Touch t in Input.touches)
         {
             if(t.TouchPhase == TouchPhase.Began)
             {
                 initialTouch = t;
             }
             else if(t.TouchPhase == TouchPhase.Moved)
             {
                 float deltaX = initialTouch.position.x - t.position.x;
                 float deltaY = initialTouch.position.y - t.position.y;
                 distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
                 
                 if (distance > 100f)
                 {
                     paddle.transform.position.x += deltaX;
                     hasSwiped = true;
                 }
             }
             else if(t.TouchPhase == TouchPhase.Ended)
             {
                 initialTouch = new Touch();
                 hasSwiped = false;
             }
         }
     }
 }

I am also a C# noobie so I am not sure if this would work, but try it, and if other members want to correct my code, they can. :)

EDIT: I think the code at line 34 is not going to work, if so, try instead:

 if (distance > 100f)
         {
             //If you want the paddle to follow the mouse on X and Y, do:
             Vector3 paddlePositionXY = new Vector3 (deltaX, deltaY, 0);
             paddle.transform.position = paddlePositionXY;
 
             //If you don't want the paddle to follow the mouse on Y, do:
             Vector3 paddlePositionX = new Vector3 (deltaX, 0, 0);
             paddle.transform.position = paddlePositionX;
 
             hasSwiped = true;
         }
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 Firzok · Aug 04, 2015 at 02:42 PM 0
Share

alt text

Thanks for the reply unfortunately the code doesn't work.

untitled.png (39.4 kB)
avatar image
0

Answer by iDownward_ · Jan 27, 2017 at 07:49 PM

@Firzok, write this code: void Update(){ float posY = gameObject.transform.position.y + (Input.GetAxis ("MouseY") * speed); playerPos = new Vector2 (transform.position.x, Mathf.Clamp (posY, minY, maxY)); gameObject.transform.position = playerPos; }

After that, don't forget to click File -> Build Settings -> Android -> Switch Platform.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Drag and DoubleTap from mouse to touch??? 0 Answers

Android Multi touches not working 1 Answer

How to Raycast on Touch? 3 Answers

it does not wait for user to touch,unity does not wait for me to touch a game object 0 Answers

The type or namespace name `Events' does not exist in the namespace `TouchScript' 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