Hi, i’m trying to do a 2d Game.
How can i set a min/max limit on the X/Y axes if I use this type of code(Movement by dragging the finger on the screen)?
There is a tentative with : &&transform.position.y < maxHeight - doesn’t work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
private Vector3 touchPosition;
private Rigidbody2D rb;
private Vector3 direction;
private float moveSpeed = 15f;
public float maxHeight;
public float minHeight;
// Use this for initialization
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void Update()
{
if (Input.touchCount > 0 && transform.position.y < maxHeight || transform.position.y > minHeight)
{
Touch touch = Input.GetTouch(0);
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0;
direction = (touchPosition - transform.position);
rb.velocity = new Vector2(direction.x, direction.y) * moveSpeed;
if (touch.phase == TouchPhase.Ended)
rb.velocity = Vector2.zero;
}
}
}