Hi All, i have a problem: i have a main cube in which i have a camera and other small cubes a sort of 3d desktop).
The cubes are obviously the icons.
I have a function tha drag them but they exit fron the walls around.
All the itema are non gravity and non kinematic rigidbody and the code i use for dragging is;
using UnityEngine;
using System.Collections;
public class MoveIcons : MonoBehaviour
{
Vector3 initPos;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
initPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
void OnMouseDrag ()
{
float distance_to_screen = Camera.main.WorldToScreenPoint (gameObject.transform.position).z;
transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
}
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Right")
{
Debug.Log("Right");
transform.position = initPos;
}
}
}
How can i stop or reset the position of the icon while dragging if it collides with the walls?