Kod:
- Kod: Zaznacz wszystko
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cube : MonoBehaviour
{
Vector2 initPos;
Vector2 targetPos;
Vector2 MinPos;
Vector2 MaxPos;
public GameObject Cube;
// Start is called before the first frame update
void Start()
{
initPos = transform.position;
targetPos = initPos;
Vector2 Size = GetComponent<SpriteRenderer>().bounds.extents;
MinPos = (Vector2)Camera.main.ViewportToWorldPoint(new Vector2(0, 0)) + Size;
MaxPos = (Vector2)Camera.main.ViewportToWorldPoint(new Vector2(1, 1)) - Size;
}
// Update is called once per frame
void Update()
{
Vector2 targetPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
if (Input.GetMouseButton(0))
{
targetPos.x = Mathf.Clamp(targetPos.x, MinPos.x, MaxPos.x);
targetPos.y = Mathf.Clamp(targetPos.y, MinPos.y, MaxPos.y);
transform.position = targetPos;
}
Cube to ten Sprite o którym wcześniej pisałem
Mogę również podesłać nagranie, ale nie wiem czy tutaj mogę (na discordzie mogę wysłać)