在范围区域 Unity 中移动对象
Move object in range zone Unity
有一个对象 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);
}
}
}
要移动对象,我使用 Touch 和 deltaPosition。 minX,maxX 为屏幕范围。如果对象的位置在 minX/1.39 和 maxX/1.41 范围内,我想移动对象。问题是,如果对象到达范围,它就会停止,我补充说,如果它到达范围,用户将它移到另一边,但他仍然可以将它移出范围。我该如何解决?
//get delta
var delta = Input.GetTouch(0).deltaPosition;
//get position
var pos = lup.transform.position;
//add delta with clamp
pos.x = Mathf.Clamp(pos.x + delta.x, minX, maxX);
pos.y = Mathf.Clamp(pos.y + delta.y, minY, maxY);
//set pos
lup.transform.position = pos;
有一个对象 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);
}
}
}
要移动对象,我使用 Touch 和 deltaPosition。 minX,maxX 为屏幕范围。如果对象的位置在 minX/1.39 和 maxX/1.41 范围内,我想移动对象。问题是,如果对象到达范围,它就会停止,我补充说,如果它到达范围,用户将它移到另一边,但他仍然可以将它移出范围。我该如何解决?
//get delta
var delta = Input.GetTouch(0).deltaPosition;
//get position
var pos = lup.transform.position;
//add delta with clamp
pos.x = Mathf.Clamp(pos.x + delta.x, minX, maxX);
pos.y = Mathf.Clamp(pos.y + delta.y, minY, maxY);
//set pos
lup.transform.position = pos;