【问题标题】:how to pass instantiated object to camera如何将实例化对象传递给相机
【发布时间】:2016-12-10 15:26:06
【问题描述】:

我正在我的场景中实例化一个对象,现在我希望相机跟随它我该怎么做? 对象已实例化,但我不知道如何将对象传递给我的相机目标; 而且你不能将实例化的对象拖到检查器

public class TankManager : MonoBehaviour
{
void Start()
{
    Instantiate (MenuManager.SelectedCharacter, Vector3.zero, Quaternion.identity);
}}

我的相机代码:

public class FollowCamera : MonoBehaviour{
public Transform target;
public Vector3 offsetPosition;
public Space offsetPositionSpace = Space.Self;
public bool lookAt = true;

private void Update()
{
    Refresh();
}

public void Refresh()
{
    if(target == null)
    {
        Debug.LogWarning("Missing target ref !", this);

        return;
    }

    if(offsetPositionSpace == Space.Self)
    {
        transform.position = target.TransformPoint(offsetPosition);
    }
    else
    {
        transform.position = target.position + offsetPosition;
    }

    if(lookAt)
    {
        transform.LookAt(target);
    }
    else
    {
        transform.rotation = target.rotation;
    }
}

}

【问题讨论】:

    标签: c# unity3d camera parameter-passing


    【解决方案1】:

    您的目标变量是公开的,因此请在 TankManager 脚本中对其进行引用。 假设您将 TankManager 脚本附加到层次结构中的游戏对象,该脚本应如下所示:

    public class TankManager : MonoBehaviour
    {
    
    public Camera camera;
    FollowCamera followC;
    
        void Start()
        {
            GameObject target = Instantiate (MenuManager.SelectedCharacter, Vector3.zero, Quaternion.identity);
            followC = camera.GetComponent <FollowCamera>();
            followC.target = target;
       }
    }
    

    然后只需将相机拖放到 TankManager 检查器区域即可。

    【讨论】:

    • 感谢兄弟,它工作了;但是对于那些阅读游戏对象的人来说,应该进行一点改进,它也需要一个演员;
    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 2011-10-09
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多