在范围区域Unity中移动对象

时间:2018-06-16 19:48:02

标签: c# unity3d move

有一个对象lup,我想在某个屏幕范围内移动它。有这样的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Touch2 : MonoBehaviour {
    public float speed = 2.5F;
    public GameObject lup;
    public Text text;
  float minX ,maxX, minY, maxY;
//private float minX, minY, maxX, maxY;
void Start(){
    minX = Camera.main.ScreenToWorldPoint(new Vector2(0f, 0f)).x + 0.1f;
    minY = Camera.main.ScreenToWorldPoint(new Vector2(0f, 0f)).y + 0.1f;
    maxX = -minX;
    maxY = -minX;

    //lup.GetComponent<Renderer>().enabled = false;

}
public void Deffault(){
    lup.transform.position = new Vector3(0, 0, 0);
}
public void Update(){

    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {     
        Touch touch = Input.GetTouch (0);
        Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
        //text.text = "x:" + lup.transform.position.x.ToString () + " " + "y:" + lup.transform.position.y.ToString ();
        if (lup.transform.position.x > minX / 1.39f && lup.transform.position.x < maxX / 1.41f) {

            lup.GetComponent<Renderer> ().enabled = true;
            //Vector3 pos = Input.mousePosition;
            //transform.position = pos;
            transform.Translate (touchDeltaPosition.x * speed, touchDeltaPosition.y, 0);
        } else if (lup.transform.position.x < minX / 1.39f || lup.transform.position.x > maxX / 1.41f) {
            transform.Translate (-touchDeltaPosition.x * speed + 0.5f, -touchDeltaPosition.y, 0);
        }
    }
    if (Input.GetTouch (0).phase == TouchPhase.Ended) {
        lup.GetComponent<Renderer>().enabled = false;
        //lup.transform.position = new Vector3(minX*2.0f, maxY/1.5f, 0);
    }
}
}

要移动ojbect,请使用Touch和deltaPosition。 minX,maxX是屏幕的范围。如果他的位置在minX / 1.39和maxX / 1.41范围内,我想移动对象。问题是,如果对象到达它停止的范围,并且我添加了else,如果它到达范围,用户将其移动到另一侧,但他仍然可以将其移出范围。我怎么解决它?

1 个答案:

答案 0 :(得分:0)

df.to_sql('table_name', redshiftEngine, index = False, if_exists = 'replace' )