Question by
Bakausagi-chan · Apr 10, 2018 at 11:44 PM ·
cameradistancelimit
Unity2D: Limit the travel distance of my player to half the size of the cameras width.
Hi, I'm having troubles trying to limit the distance of my player from leaving a certain area, I want to shorten the distance to half the width of my main camera, is there a way to do so, this is my code:
void Update () {
if (GameObject.Find ("GameCanvas").GetComponent<GameManager> ().Usermove) {
if (GameControl.GetComponent<GameController> ().choosenSetting == true) { // Touch Method
if (Input.GetMouseButtonDown (0)) {
if (EventSystem.current.currentSelectedGameObject)
return;
Vector3 mousePosition = Input.mousePosition - target;
mousePosition.z = 10;
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.y = transform.position.y;
audioClip.Play ();
isMoving = true;
if (tapCooler > 0 && tapCount == 1) {
playerDirection = new Vector2 (Input.mousePosition.x, Screen.height - Input.mousePosition.y);
var playerPoint = Camera.main.WorldToScreenPoint (player.transform.position);
if (isMoving == true) {
if (playerDirection.x < playerPoint.x) {
par2.Play ();
par1.Stop ();
} else {
par1.Play ();
par2.Stop ();
}
}
} else {
tapCooler = 0.2f;
tapCount += 1;
}
} else {
isMoving = false;
moving = false;
}
target = Vector3.Lerp (transform.position, target, Distance);
transform.position = Vector3.MoveTowards (transform.position, target, moveSpeed * Time.deltaTime);
}
Thank you!
Comment
Your answer
Follow this Question
Related Questions
Pixel to z-Distance in Unity 0 Answers
Distance between two objects, from the view of the camera. 0 Answers
Modify Draw Distance via Script? 1 Answer
limit unity editor camera to 2d space 0 Answers