我正在统一制作一个游戏,用户拖动以向远处的某些物体射击球。到目前为止,我有这个 DragAndShoot 脚本:
//using System.Collections;
//using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Collider))]
public class DragAndShoot : MonoBehaviour
{
public Transform prefab;
private Vector3 mousePressDownPos;
private Vector3 mouseReleasePos;
private Rigidbody rb;
private bool isShoot;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnMouseDown()
{
mousePressDownPos = Input.mousePosition;
}
private void OnMouseUp()
{
mouseReleasePos = Input.mousePosition;
Shoot(mouseReleasePos-mousePressDownPos);
}
private float forceMultiplier = 3;
void Shoot(Vector3 Force)
{
if(isShoot)
return;
rb.AddForce(new Vector3(Force.y,Force.x,Force.z) * forceMultiplier);
isShoot = true;
createBall();
}
void createBall(){
Instantiate(prefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
}
}
如您所见,我创建了函数 createBall() 以便在游戏对象 SpawnPoint 的位置重新生成一个球预制件。当我运行游戏时,第一个球射得很好。另一个球重生。
问题:当我射出第二个球并移动时,第二个球似乎又出现了一个球,并且它也移动了。不知道为什么会发生这种情况以及如何解决它 - 有人可以帮忙吗?谢谢。