Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Mikibey · Feb 14, 2016 at 06:16 PM · c#androidobjecttouchlimitations

C# Limit touch movement on a raycast

Hello everyone, i've found a script that makes my object draggable. The problem is that i want the object to be dragged only in a certain space in the screen. For example on the x - Axis : -2.2f and 2.2f

I've modified the script but when i touch the object it goes to the bottom of the screen. Can you tell me what am i doing wrong ? It's my first time working with Touch input.

Here is my code :

 using UnityEngine;
 using System.Collections;
 
 public class Drag2 : MonoBehaviour {
     private float dist;
     private bool dragging = false;
     private Vector3 offset;
     private Transform toDrag;
     
     void Update() {
         Vector3 v3;
         
         if (Input.touchCount != 1) {
             dragging = false; 
             return;
         }
         
         Touch touch = Input.touches[0];
         Vector3 pos = touch.position;
         
         if(touch.phase == TouchPhase.Began) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(pos); 
             if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
             {
                 toDrag = hit.transform;
                 dist = hit.transform.position.z - Camera.main.transform.position.z;
                 v3 = new Vector3(pos.x, pos.y, dist);
                 v3 = Camera.main.ScreenToWorldPoint(v3);
                 offset = toDrag.position - v3;
                 dragging = true;
             }
         }
         if (dragging && touch.phase == TouchPhase.Moved) {
 /// Here i try to clamp my values
             float testx = Mathf.Clamp(Input.mousePosition.x, -2.2f, 2.2f);
             float testy = Mathf.Clamp(Input.mousePosition.y, -2.2f, 2.2f);
             v3 = new Vector3(testx, testy, dist);
             v3 = Camera.main.ScreenToWorldPoint(v3);
             toDrag.position = v3 + offset;
 ////////////////////////////////////////////////////
         }
         if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
             dragging = false;
         }
     }
 }
Comment
Add comment · Show 2
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 Mikibey · Feb 15, 2016 at 04:01 AM 0
Share

Bump please i need some help here :(

avatar image Mikibey · Feb 15, 2016 at 08:07 AM 0
Share

I've also tried this but it's not working...i'm running out of ideas....

 using UnityEngine;
 using System.Collections;
 
 public class Drag2 : $$anonymous$$onoBehaviour {
     private float dist;
     private bool dragging = false;
     Vector3 offset;
     private Transform toDrag;
     
     void Update() {
         Vector3 v3;
         
         if (Input.touchCount != 1) {
             dragging = false; 
             return;
         }
         
         Touch touch = Input.touches[0];
         Vector3 pos = touch.position;
         
         if(touch.phase == TouchPhase.Began) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(pos); 
             if(Physics.Raycast(ray, out hit) && (hit.collider.tag == "Draggable"))
             {
                 toDrag = hit.transform;
                 hit.transform.position = new Vector3($$anonymous$$athf.Clamp(hit.transform.position.x, -2.5f, 2.5f), $$anonymous$$athf.Clamp(hit.transform.position.y, -2.5f, 2.5f), hit.transform.position.z);
                 dist = hit.transform.position.z - Camera.main.transform.position.z;
                 v3 = new Vector3($$anonymous$$athf.Clamp(pos.x, -2.5f, 2.5f), $$anonymous$$athf.Clamp(pos.y, -2.5f, 2.5f), dist);
                 v3 = Camera.main.ScreenToWorldPoint(v3);
                 offset = toDrag.position - v3;
                 dragging = true;
             }
         }
         if (dragging && touch.phase == TouchPhase.$$anonymous$$oved) {
             v3 = new Vector3($$anonymous$$athf.Clamp(Input.mousePosition.x, -2.5f, 2.5f), $$anonymous$$athf.Clamp(Input.mousePosition.y, -2.5f, 2.5f), dist);
             v3 = Camera.main.ScreenToWorldPoint(v3);
             toDrag.position = v3 + offset;
         }
         if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)) {
             dragging = false;
         }
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mikibey · Feb 14, 2016 at 06:39 PM

Can anyone help me out? :D

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 Captain_Command · May 02, 2019 at 05:27 PM

@Mikibey did you find a solution to this? I am having a similar problem.

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

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

Multi-touch game can only shoot 1 ball at a time 0 Answers

Touch position inacurrate 0 Answers

Problem with simple Character movement on Android (Touch/GetMouseButtonDown) 1 Answer

simple touch input test isn't working 0 Answers

How can i move just one object ? 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