楼上要的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject go;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//2D射线碰撞检测
var hit = Physics2D.Raycast(ray.origin, Vector2.zero);
//输出射线起点
Debug.Log(new Vector2(ray.origin.x, ray.origin.y).ToString());
//显示射线
Debug.DrawRay(ray.origin, ray.direction * 20f, Color.blue, duration: 3.0f);
//---
// RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (Input.GetMouseButtonDown(1))
{
// if (hit.collider)
{
if (hit.collider.name== "Square")
{
GameObject Gos = Instantiate(go, transform.position, Quaternion.identity) as GameObject;
Gos.transform.position = new Vector3(ray.origin.x, ray.origin.y+10, 0);
}
}
}
}
}