[英]How to Spawn Objects on Mouse Path? Unity 3D
这就是我想要实现的目标:
移动鼠标(鼠标按钮按下)时,我想保存整个鼠标移动,我的意思是从开始(单击按钮)点到结束(释放按钮)点。 释放按钮后,我想在整个保存的鼠标路径上生成一些对象(如图所示),并且每两个生成的对象之间的空间应该相同。
我怎样才能在代码中做到这一点? 我已经开始了:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
}
if (Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0))
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z);
Vector3 curPosition = new Vector3(-Camera.main.ScreenToWorldPoint(curScreenPoint).x, -Camera.main.ScreenToWorldPoint(curScreenPoint).y, transform.position.z);
if ((Input.GetAxis("Mouse X") != 0) || (Input.GetAxis("Mouse Y") != 0));
{
GameObject TheBall = Instantiate(Ball, curPosition, Quaternion.identity);
TheBall.transform.SetParent(transform, false);
}
}
就像我说的,你首先可能会得到每个点之间的平均空间。 之后,您将始终从之前的位置创建新点。 你可以这样做:
public GameObject prefab;
List<Vector2> points = new List<Vector2>();
void Update() {
// You add the points in update to the List
if(Input.GetMouseButtonUp(0)) {
float totalDistance = 0;
for(int i = 0; i < points.Count-1; i++) {
totalDistance += Vector2.Distance(points[i], points[i+1]);
}
float averageDistance = totalDistance / (points.Count-1);
// Spawning first Point
Vector2 lastPoint = points[0];
Instantiate(
prefab,
new Vector3(lastPoint.x, z, lastPoint.y),
Quaternion.identity
);
// Spawning other Points
for(int i = 0; i < points.Count - 1; i++) {
Vector2 spawnPos = lastPoint + ((points[i+1] - lastPoint).normalized * averageDistance)
lastPoint = spawnPos; //Save the new point because you want create the next one from this one
int z = 0; // The z you need
Instantiate(
prefab,
new Vector3(spawnPos.x, z, spawnPos.y),
Quaternion.identity
);
}
}
}
编辑:为您创建了代码。 您的启动代码也有一些问题,但这在这里是有效的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnCircles : MonoBehaviour {
public GameObject prefab;
public float Y;
private Camera cam;
private List<Vector2> points = new List<Vector2>();
void Update() {
if(Input.GetMouseButton(0)) {
cam = Camera.main;
Vector2 mousePos = new Vector2();
mousePos.x = Input.mousePosition.x;
mousePos.y = cam.pixelHeight - Input.mousePosition.y;
Vector3 point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 10));
points.Add(new Vector2(point.x, point.z * -1));
}
if(Input.GetMouseButtonUp(0)) {
float totalDistance = 0;
for(int i = 0; i < points.Count - 1; i++) {
totalDistance += Vector2.Distance(points[i], points[i + 1]);
}
float averageDistance = totalDistance / (points.Count - 1);
// Spawning first Point
Vector2 lastPoint = points[0];
Instantiate(
prefab,
new Vector3(lastPoint.x, Y, lastPoint.y),
Quaternion.identity
);
// Spawning other Points
for(int i = 0; i < points.Count - 1; i++) {
Vector2 spawnPos = lastPoint + ((points[i + 1] - lastPoint).normalized * averageDistance);
lastPoint = spawnPos; //Save the new point because you want create the next one from this one
Instantiate(
prefab,
new Vector3(spawnPos.x, Y, spawnPos.y),
Quaternion.identity
);
}
// Resetting List
points = new List<Vector2>();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.