0
votes

Hi I have a sprite that moves around the screen with left click of mouse, the problem I have is that the sprite can be dragged off scene. I believe that the use of mathf and clamp but cannot figure how to insert it into this script.

tried inserting a basic mathf.clamp script on to sprite but the result was that the sprite would flick between the top and bottom of my clamp really quickly.

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class DragMove : MonoBehaviour {

public GameObject gameObjectToDrag; // refer to Go that being dragged

public Vector3 Gocenter; // gameobject centre
public Vector3 touchPosition; // touch or click position
public Vector3 offSet; // vector between touchpoint/mouse click to the object centre
public Vector3 newGOCenter; // new center of object

RaycastHit hit; // store hit object information

public bool draggingmode = false; //   



// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update()
{

    //********************************
    // **Click to Drag****************
    //********************************

if UNITY_EDITOR

Thank you for any help

1

1 Answers

0
votes

I used this bit of code

 if (newGOCenter.y <= 1)
                    {
                        newGOCenter.y = 1;
                    }
                    if (newGOCenter.y >= 5)
                    {
                        newGOCenter.y = 5;
                    }
                    if (newGOCenter.x <= 1)
                    {
                        newGOCenter.x = 1;
                    }
                    if (newGOCenter.x >= 9)
                    {
                        newGOCenter.x = 9;
                    }