2020-04-19 15:55:44 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
|
|
|
public class FollowMouse : MonoBehaviour
|
|
|
|
|
{
|
2020-04-19 16:26:30 +00:00
|
|
|
|
public Vector2 min;
|
|
|
|
|
public Vector2 max;
|
|
|
|
|
|
2020-04-19 15:55:44 +00:00
|
|
|
|
private new Rigidbody2D rigidbody2D;
|
|
|
|
|
|
|
|
|
|
void Awake() {
|
|
|
|
|
rigidbody2D = GetComponent<Rigidbody2D>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Start() {
|
|
|
|
|
Vector3 screenPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
2020-04-19 16:26:30 +00:00
|
|
|
|
transform.position = new Vector3(Mathf.Clamp(screenPoint.x, min.x, max.x), Mathf.Clamp(screenPoint.y, min.y, max.y), transform.position.z);
|
2020-04-19 15:55:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
Vector3 screenPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
2020-04-19 16:26:30 +00:00
|
|
|
|
rigidbody2D.MovePosition(new Vector3(Mathf.Clamp(screenPoint.x, min.x, max.x), Mathf.Clamp(screenPoint.y, min.y, max.y), transform.position.z));
|
2020-04-19 15:55:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|