【问题标题】:How to reference camera in prefab script in Unity 2D?如何在 Unity 2D 的预制脚本中引用相机?
【发布时间】:2023-01-27 00:53:11
【问题描述】:

我已经创建了一个玩家预制件(在我的项目中称为 Tim),并试图对 gameObjects 进行所有引用,并直接从实际上附加到作为玩家预制件子项的枪支对象的玩家脚本之一进行转换。

问题是我无法设法在脚本中引用相机,尽管我已经查看并尝试了许多不同的方法,但似乎都不起作用。尽管 Unity 在控制台中打印此错误:“NullReferenceException:对象引用未设置为对象的实例”。这是脚本:

public class Gun_Control : MonoBehaviour
{
// References for GameObjects
[SerializeField] private Rigidbody2D rb;
private GameObject Player;
[SerializeField] private Transform PlayerTransform;
private GameObject Gun;
[SerializeField] private Transform GunTransform;
private Camera MainCamera;
private GameObject firePoint;
[SerializeField] private Transform firePointTransform;
[SerializeField] private GameObject bulletPrefab;

// Variables for Shooting
private Vector2 mousePos;
private float bulletForce = 20f;

// Start is called at the beginning
void Start()
{
    Debug.Log("Starting");
    Player = GameObject.FindWithTag("Player");
    PlayerTransform = Player.transform;
    Gun = GameObject.FindWithTag("PlayerGun");
    GunTransform = Gun.transform;
    MainCamera = GameObject.FindWithTag("Camera").GetComponent<Camera>();
    firePoint = GameObject.FindWithTag("PlayerFirePoint");
    firePointTransform = firePoint.transform;
}

// Update is called once per frame
void Update()
{
    // Get mouse position
    mousePos = MainCamera.ScreenToWorldPoint(Input.mousePosition);

    // Run shoot function on left click
    if(Input.GetButtonDown("Fire1"))
    {
        Shoot();
    }
}

// Update is called on every physics frame
void FixedUpdate()
{
    // Set gun position to player position
    GunTransform.position = PlayerTransform.position;
    // Set gun rotation to mouse position
    Vector2 lookDir = mousePos - rb.position;
    float angle = Mathf.Atan2(lookDir.y ,lookDir.x) * Mathf.Rad2Deg - 180f;
    rb.rotation = angle;
}

void Shoot()
{
    // Instantiate a bullet at the firepoint and give it force
    GameObject bullet = Instantiate(bulletPrefab, firePointTransform.position, firePointTransform.rotation);
    Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    rb.AddForce(firePointTransform.up * bulletForce, ForceMode2D.Impulse);
}
}

现在我有一个变量,MainCamera,当脚本启动时,我会寻找一个以“Camera”作为标签且设置正确的相机。 如果有人需要更多详细信息,我可以补充,感谢大家花时间提供帮助。

编辑 1: 我尝试了 thunderskill 的建议,但它似乎不起作用。 这是新代码的图片。

当我尝试使用 Debug.Log(Camera.main);它打印为空。

【问题讨论】:

  • 只需调用 Camera.main 添加一个测试,如果它首先存在于场景中,你就可以开始了

标签: c# unity3d reference camera prefab


【解决方案1】:

这是访问主摄像头的一个很好的例子:

Camera m_MainCamera;
void Start()
{
        //This gets the Main Camera from the Scene
     if(Camera.main != null){
        m_MainCamera = Camera.main;
        //This enables Main Camera
        m_MainCamera.enabled = true; 
        }
}

【讨论】:

  • 我刚试过这个,但仍然有错误。它说错误在这一行:“mousePos = MainCamera.ScreenToWorldPoint(Input.mousePosition);”我在主要问题中添加了详细信息。
猜你喜欢
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多