- Home /
Question by
Davide-Barbieri · Nov 23, 2016 at 07:32 AM ·
rigidbodyclick and drag
click and drag gameObject, but when clicking it goes outside of camera
i'm trying to drag some objects (the car in the screenshot) using the mouse and i'm using this code:
public class Trascina_veicoli : MonoBehaviour {
float distance = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDrag()
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition;
}
}
but everytime i click the objects gets an high Y position because it's based on mouse position, so it goes out from camera. how can i make that when clicked on Y position doesn't change, so i can drag it without having the problem that it goes out of screen?
screenshot-1.png
(28.1 kB)
Comment
Your answer
Follow this Question
Related Questions
How to hide a Game Object on collission? 2 Answers
Make CharacterController act like rigidbody on explosions 1 Answer
How do I add force to rigidbody when raycast hits it. 1 Answer
Calculate forces required to rotate around an arbitrary point regardless of center of mass position 1 Answer
Ball falling through trigger floor 2 Answers