【发布时间】:2021-07-30 19:33:11
【问题描述】:
所以我想知道如何在 Unity3D 中实例化一个没有交集的 GameObject。到目前为止,球体实例与我用射线投射击中的对象相交。我正在使用 hit.point 的位置,但想知道是否有办法在碰撞而不是对象的原点上生成它。这是我的参考代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallSpawn : MonoBehaviour
{
public int castLength = 100;
public GameObject spawnObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Input.GetMouseButton for infinite
if (Input.GetMouseButton(0))
{
SpawnBall();
}
}
public void SpawnBall()
{
RaycastHit hit;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * castLength, Color.green, Mathf.Infinity);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, castLength))
{
Instantiate(spawnObject, hit.point, hit.transform.rotation);
}
}
}
截至目前,球体会夹入地面并在生成时弹开。可以这么说,我希望球体被“放置”,以便它们在没有剪裁的情况下生成。
【问题讨论】:
标签: c# unity3d instantiation