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;
}
}
}
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;
}
}
}
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.
Your answer
Follow this Question
Related Questions
Change object scale by touch (in game) ^ 0 Answers
Why is my multi touch not working? 1 Answer
Jump by touch Unity C# 0 Answers
Player follow touch when holding down your finger on the screen (C#) 1 Answer