已解决的问题:确保您的预制件位置是0/0/0新秀错误...感谢那些提供帮助的人。
因此,基本上我要做的就是让一个简单的弹孔对象出现在玩家射击的立方体上。该游戏采用的第三人称视角类似于《死亡空间》。
我能够使射线广播显示在射击按钮上,并且似乎位于枪尖的中央,而摄像机正是我想要的。
问题是,当我尝试实例化此bulletHole预制件时,它出现在中间的右上角。它有些偏离了,我不知道为什么。
using UnityEngine;
public class Gun : MonoBehaviour
{
/* Below are variables that we can access and that the gun will be using */
public float range = 1000f;
/* Below is the camera the user is viewing through and the bullet hole prefab. */
public GameObject BulletHolePrefab;
public Camera cam;
void Start()
{
}
// Update is called once per frame
void Update()
{
// This presents the line in which the user is aiming with
Vector3 lineOrigin = cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
Debug.DrawRay(lineOrigin, cam.transform.forward * range, Color.green);
if (Input.GetButtonDown("Fire1"))
{
// This is the method that handles the shooting
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
// If something is hit with the Raycast it returns true.
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit))
{
GameObject newHole = Instantiate(BulletHolePrefab, hit.point, Quaternion.identity) as GameObject;
newHole.transform.up = hit.normal;
Destroy(newHole, 5f);
}
}
}
我对团结还比较陌生,但对编程还不陌生,无法解决这个问题。我已经尝试了几个小时,现在正在寻求互联网的帮助!预先感谢...
您为什么使用ViewportPointToRay()
?如果您可以使用摄像机,则只需从transform.position
发出光线,并使用transform.forward
向前发出光线。