- Home /
Limiting Drag and Drop
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour{
public static int speed = 5;
Vector3 point;
void Update(){
transform.position = new Vector3(Mathf.Clamp(Time.time, -2.5F, 2.5F), 0, 0);
}
void OnMouseDrag(){
point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.y = transform.position.y;
transform.position = point;
}
}
This is my code above.
It works witohut Mathf.Clamp thing but when I add Mathf.Clamp gameobject goes right and up.I want to limit drag and drop area.How can I do?
- I don't want to use y axis and it must be fixed.I'm working with x and z axis.
Thanks!
Answer by maccabbe · Mar 23, 2015 at 05:52 AM
First of all, you probably shouldn't use Time.time and 0 as the new position since that does not relate to the old position. It would also make sense to clamp the new position before it is applied, in the OnMouseDrag method. Try the following
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour{
void OnMouseDrag(){
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.x = Mathf.Clamp(point.x, -2.5f, 2.5f);
point.y = transform.position.y;
point.z = Mathf.Clamp(point.z, -2.5f, 2.5f);
transform.position = point;
}
}
Is doesn't work for 3D Objects. Will you please elaborate for movement of 3D Objects.
Your answer
Follow this Question
Related Questions
Drag And drop objects mouse 2 Answers
DragAndDrop.objectReferences won't accept a new array of objects 0 Answers
Error: 'transform' is not a member of 'Object' 2 Answers
Paint textures on an object 0 Answers
Rotate object with another object 0 Answers