无法在Raycast Center上显示子弹孔

问题描述 投票:0回答:1

已解决的问题:确保您的预制件位置是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);
        }
    }
}

This is what I mean, as we can see the crosshairs and raycast are centered but the bullethole appears to the tope right.

我对团结还比较陌生,但对编程还不陌生,无法解决这个问题。我已经尝试了几个小时,现在正在寻求互联网的帮助!预先感谢...

c# unity3d raycasting
1个回答
0
投票

您为什么使用ViewportPointToRay()?如果您可以使用摄像机,则只需从transform.position发出光线,并使用transform.forward向前发出光线。

© www.soinside.com 2019 - 2024. All rights reserved.