基于灯光控制移动。已启用。C#

基于灯光控制移动。已启用。C#,c#,unity3d,C#,Unity3d,在我的新项目中,我有一个机械师,敌人被编程来追逐玩家,但前提是他们启用了“火炬”。正如您将在脚本中看到的,我使用空检查来执行此操作 代码如下: public class chasePlayer : MonoBehaviour { public Transform target; public float speed; public Light playerLight; void followLight() { if (playerLigh

在我的新项目中,我有一个机械师,敌人被编程来追逐玩家,但前提是他们启用了“火炬”。正如您将在脚本中看到的,我使用空检查来执行此操作

代码如下:

public class chasePlayer : MonoBehaviour
{
    public Transform target;
    public float speed;
    public Light playerLight;

    void followLight()
    {
        if (playerLight != null)
        {
            speed = 1;
            float walkspeed = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, target.position, walkspeed);
        }
    }

    void stopFollowing()
    {
        if (playerLight = null)
        {
            speed = 0;
        }
    }

    void Update()
    {
        followLight();
        stopFollowing();
    }
}
问题是,我认为我所有的代码都是正确的,理论上它应该做我想做的事情,但事实并非如此。即使在我开始游戏的时候它也不会移动

我可能做错了什么。
第一次编写原始脚本时,可能会出现很多错误

基于您的上一次评论,请尝试以下操作:

public class chasePlayer : MonoBehaviour
{
    public Transform target;
    public float speed;
    public Light playerLight;

    void followLight()
    {
        if (playerLight != null)
        {
            speed = 1;
        }
        float walkspeed = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, walkspeed);            
    }

    void stopFollowing()
    {
        if (playerLight == null)
        {
            speed = 0;
        }
    }

    void Update()
    {
        followLight();
        stopFollowing();
    }
}

根据您最后的评论,尝试以下操作:

public class chasePlayer : MonoBehaviour
{
    public Transform target;
    public float speed;
    public Light playerLight;

    void followLight()
    {
        if (playerLight != null)
        {
            speed = 1;
        }
        float walkspeed = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, walkspeed);            
    }

    void stopFollowing()
    {
        if (playerLight == null)
        {
            speed = 0;
        }
    }

    void Update()
    {
        followLight();
        stopFollowing();
    }
}
老兵在这里*笑*

在我们开始之前,我只想提到您正在使用C#,所以请尝试使用
CamelCase
来命名方法和类

您的代码不工作,因为您正在检查
组件的
null
<代码>空值仅在未分配任何内容或已分配对象被销毁时才会出现。如果要检查组件的状态,最好使用
playerLight.enabled
。在总体代码中添加一些小的改进,现在看起来像:

public class ChasePlayer : MonoBehaviour
{
    public Light playerLight;
    public float speed;
    public Transform target;


    private void FollowLight()
    {
        // Does checking for given statement but is only executed in Debug mode.
        // Fairly good for fail-proofing private methods
        // Or clarifying, what values should be before invoking the method.
        Debug.Assert(playerLight != null);

        float walkspeed = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, walkspeed);      
    }

    private void Update()
    {
        if(playerLight != null && playerLight.enabled)
            FollowLight();
    }
}
注意:不要忘记将类文件重命名为
ChasePlayer
,因为我已经计算了类名(Unity要求文件和类名匹配,以便能够在编辑器中将组件分配给游戏对象)。

这里*笑*

在我们开始之前,我只想提到您正在使用C#,所以请尝试使用
CamelCase
来命名方法和类

您的代码不工作,因为您正在检查
组件的
null
<代码>空值仅在未分配任何内容或已分配对象被销毁时才会出现。如果要检查组件的状态,最好使用
playerLight.enabled
。在总体代码中添加一些小的改进,现在看起来像:

public class ChasePlayer : MonoBehaviour
{
    public Light playerLight;
    public float speed;
    public Transform target;


    private void FollowLight()
    {
        // Does checking for given statement but is only executed in Debug mode.
        // Fairly good for fail-proofing private methods
        // Or clarifying, what values should be before invoking the method.
        Debug.Assert(playerLight != null);

        float walkspeed = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, walkspeed);      
    }

    private void Update()
    {
        if(playerLight != null && playerLight.enabled)
            FollowLight();
    }
}

注意:不要忘记将类文件重命名为
ChasePlayer
,因为我已经计算了类名(Unity要求文件和类名匹配,以便能够在编辑器中将组件分配给游戏对象)。

有错误吗?您确定
playerLight
不是
null
?在
stopFollowing()
中,这是一个打字错误还是您真的有
playerLight=null
?它应该是
playerLight==null
。。否则,您会将您的
playerLight
alsways设置为
null
,因此它不会移动谢谢您,这是一个错误,但现在即使在开始时移动,“敌人”也不会在灯光关闭时停止移动。你知道这可能是什么原因吗?假设灯被关掉了。在我看来,从stopFollowing()方法可以看出,如果关闭了playerLight变量,它将等于null。因此,将速度变量设置为0,一切正常。但是,下次进入followLight()方法时,light变量仍然为null。这意味着您永远不会在followLight()方法中输入if语句,因此永远不会使用speed变量进行新的速度计算。我理解您的意思,但我想不出解决方法。我把所有的灯都调为空,并带有一个空的start()stopFollowing();在另一个脚本中,在开始时将灯光更改为关闭。但是敌人仍然在追我。
if(playerLight=null)
应该是
if(playerLight==null)
否则你将
playerLight
设置为null,而不是测试它是否为null。有错误吗?您确定
playerLight
不是
null
?在
stopFollowing()
中,这是一个打字错误还是您真的有
playerLight=null
?它应该是
playerLight==null
。。否则,您会将您的
playerLight
alsways设置为
null
,因此它不会移动谢谢您,这是一个错误,但现在即使在开始时移动,“敌人”也不会在灯光关闭时停止移动。你知道这可能是什么原因吗?假设灯被关掉了。在我看来,从stopFollowing()方法可以看出,如果关闭了playerLight变量,它将等于null。因此,将速度变量设置为0,一切正常。但是,下次进入followLight()方法时,light变量仍然为null。这意味着您永远不会在followLight()方法中输入if语句,因此永远不会使用speed变量进行新的速度计算。我理解您的意思,但我想不出解决方法。我把所有的灯都调为空,并带有一个空的start()stopFollowing();在另一个脚本中,在开始时将灯光更改为关闭。但是敌人仍然在追我。
if(playerLight=null)
应该是
if(playerLight==null)
否则你将
playerLight
设置为null,而不是测试它是否为null。抱歉,不幸的是,这也不起作用。不管怎样,我都想不出解决办法。我可能应该等一个老兵来帮我。非常感谢你们两位试图帮助我:)如果你们能找到解决这个问题的方法,尽管我会非常感激:))对不起,不幸的是,这也不起作用。不管怎样,我都想不出解决办法。我可能应该等一个老兵来帮我。非常感谢你们两位试图帮助我:)如果你们能想出一个解决这个问题的方法,尽管我会非常感激:))有趣的是,我唯一需要的是几句台词和一些小的改进来让它工作。这两个小时的错误我不会再犯了,多亏了你。你是一个伟大的程序员,享受你的一周:)谢谢你的热情回应。这真的让我心情振奋!有趣的是,我唯一需要的就是几行文字和一些小的改进来达到这个目的