Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么赢了';我的NPC胶囊不会到处乱跑吗?_C#_Unity3d - Fatal编程技术网

C# 为什么赢了';我的NPC胶囊不会到处乱跑吗?

C# 为什么赢了';我的NPC胶囊不会到处乱跑吗?,c#,unity3d,C#,Unity3d,这是我第一次尝试制作类似“AI”的东西。基本上,我只想让一个太空舱偶尔在世界上选择一个位置,raycast看看是否有任何障碍,如果没有,然后移动到那个位置 我的第一次尝试如下所示,除了没有WaitForSeconds收益率和徘徊的去盎司。这一版本使胶囊锯齿状地、非常缓慢地向一个点移动。我的理论是,这可能是由于机器人试图同时“游荡”多次而导致的,因此会出现去抖动 我也尝试过使用较高/较低的“npcSpeed”,但没有效果。以下是我的代码: using System.Collections; usi

这是我第一次尝试制作类似“AI”的东西。基本上,我只想让一个太空舱偶尔在世界上选择一个位置,raycast看看是否有任何障碍,如果没有,然后移动到那个位置

我的第一次尝试如下所示,除了没有WaitForSeconds收益率和徘徊的去盎司。这一版本使胶囊锯齿状地、非常缓慢地向一个点移动。我的理论是,这可能是由于机器人试图同时“游荡”多次而导致的,因此会出现去抖动

我也尝试过使用较高/较低的“npcSpeed”,但没有效果。以下是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class npcController : MonoBehaviour {

    CharacterController control;
    Vector3 movement;
    bool isWandering = false;

    public float npcSpeed = 3f;


    void Start(){
        control = GetComponent<CharacterController> ();
    }

    void Update(){

        if (Random.Range (1f, 10f) > 8f && isWandering == false) {
            isWandering = true;
            Wander ();
        }

    }

    public bool IsWanted() {
        return true;
    }

    IEnumerator Wander(){

        int randX = Random.Range (1, 10);
        int randZ = Random.Range (1, 10);
        float moveDistance = Mathf.Sqrt (randX ^ 2 + randZ ^ 2);
        float moveTime = moveDistance / npcSpeed;

        Vector3 check = new Vector3 (randX, 0, randZ);

        RaycastHit hit;

        if (!Physics.Raycast (transform.position, check, moveDistance)) {

            transform.LookAt (check);

            control.SimpleMove (Vector3.forward * moveTime);

            yield return new WaitForSeconds (moveTime);

            isWandering = false;

        }

    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类NPC控制器:单行为{
字符控制器控制;
矢量3运动;
布尔游荡=假;
公共浮点数npcSpeed=3f;
void Start(){
控件=GetComponent();
}
无效更新(){
if(Random.Range(1f,10f)>8f&&iswanting==false){
isWandering=true;
漫游();
}
}
公共图书馆{
返回true;
}
IEnumerator Wander(){
int randX=随机范围(1,10);
int randZ=随机范围(1,10);
float moveDistance=Mathf.Sqrt(randX^2+randZ^2);
浮动移动时间=移动距离/npcSpeed;
Vector3检查=新Vector3(randX,0,randZ);
雷卡斯特击中;
如果(!physical.Raycast(transform.position,check,moveDistance)){
transform.LookAt(检查);
control.SimpleMove(Vector3.forward*moveTime);
返回新的WaitForSeconds(moveTime);
isWandering=false;
}
}
}
我试图移动的游戏对象是一个带有CharacterController和npcController脚本的胶囊


编辑:为了确认,锯齿状移动不是由任何帧下降引起的。游戏一直以170fps以上的速度运行

你不能简单地用名字来称呼协同程序。您必须使用
start例程(Wander()),这将为您启动枚举。

由于您的去盎司,Wander()只在一帧中被调用,因此您的播放器只在一帧中移动。它不会被再次调用,直到你的计时器运行,这可能是为什么你会得到紧张的运动。但是,您需要在这里进行大量的返工,因为如果您更改它,以便在
isWandering==true
时每一帧调用Wander(),则yield语句将“叠加”,您可能会得到一些不需要的结果。您可以在这里不使用计时器的协同程序就可以离开,并且可以在
iswalking==true
时移动角色

int randX, randZ;
float moveDistance, moveTime;
Vector3 check;
void Update()
{
    if (Random.Range(1f, 10f) > 8f && isWandering == false)
    {
        randX = Random.Range (1, 10); // movement parameters are set once here
        randZ = Random.Range (1, 10);
        moveDistance = Mathf.Sqrt (randX ^ 2 + randZ ^ 2);
        moveTime = moveDistance / npcSpeed;
        check = new Vector3 (randX, 0, randZ);
        isWandering = true;
    }
    if (isWandering) // Wander every frame while true
        Wander();
}

float timer;
void Wander()
{
    timer += Time.deltaTime;

    if (timer < moveTime) // move until timer exceeds movetime
    {
        RaycastHit hit;
        if (!Physics.Raycast (transform.position, check, moveDistance)) 
        {
           transform.LookAt (check);
           control.SimpleMove (Vector3.forward * moveTime); 
        }    
    }
    else
    {
        timer = 0; //reset timer and debounce
        isWandering = false;
    }
}
int randX,randZ;
浮动移动距离、移动时间;
向量3检验;
无效更新()
{
if(随机范围(1f,10f)>8f&&iswanting==false)
{
randX=Random.Range(1,10);//此处设置一次移动参数
randZ=随机范围(1,10);
moveDistance=Mathf.Sqrt(randX^2+randZ^2);
移动时间=移动距离/npcSpeed;
检查=新矢量3(randX,0,randZ);
isWandering=true;
}
if(isWandering)//在为true时每帧漫游一次
漫游();
}
浮动定时器;
虚空漫游()
{
timer+=Time.deltaTime;
if(timer
如果您将Wander()移到if语句之外,并让它仅调用
if(isWandering==true)
,这能解决问题吗?这不是问题的解决方案,但我担心
^
操作符不会做您认为它能做的事情。它不是幂,而是按位异或,而是使用
Mathf.Pow(float base,float index)
(或者对于简单的平方,只使用n*n)。看:@ryemos不太明白你在说什么mean@yes啊,好的,非常感谢!